We help IT Professionals succeed at work.

Check out our new AWS podcast with Certified Expert, Phil Phillips! Listen to "How to Execute a Seamless AWS Migration" on EE or on your favorite podcast platform. Listen Now

x

parsing xml to hashmap - weird

static86
static86 asked
on
Medium Priority
461 Views
Last Modified: 2012-05-11
Hi,
I'm getting weird error and I don't know why. I'm parsing xml using sax from this tutorial.
I want to add url as key, and keywords seperated with"|" as value to a hashmap.
  public void endElement(String uri, String localName,
            String qName) throws SAXException {



        if (qName.equalsIgnoreCase("webProfile")) {
            //add it to the list
          //  System.out.println("dodaje cos, nowy obiekt pewnie");


        } else if (qName.equalsIgnoreCase("webUrl")) {
            
            keywords.delete(0, keywords.length());

            System.out.println("deleting temp keywords");
            url=tempVal;
            System.out.println(url+"url address");

            
        } else if (qName.equalsIgnoreCase("keyWord")) {
           // System.out.println(tempVal);
            keywords.append(tempVal+"|");
            System.out.println(url+"<--addidng this url, and keywords ---> "+keywords);
            hMap.put(url,keywords);
           
        } else {
            System.out.println("final map: "+hMap); 
            new Parser().sendRequest(hMap);
        }
       
    }

Open in new window

But when I'm parsing this xml: http://webtools.drynski.com:8080/WebTools/keywords/api/user?pass=user the first url gets somehow keywords from second url. I don't unrerstand why:/.
Here is my println:
deleting temp keywords
http://www.hosthelp.plurl address
http://www.hosthelp.pl<--addidng this url, and keywords ---> opinie hosting|
http://www.hosthelp.pl<--addidng this url, and keywords ---> opinie hosting|drugi keyword|
deleting temp keywords
http://www.wp.plurl address
http://www.wp.pl<--addidng this url, and keywords ---> kulka|
http://www.wp.pl<--addidng this url, and keywords ---> kulka|bananahamak|
final map: {http://www.hosthelp.pl=kulka|bananahamak|, http://www.wp.pl=kulka|bananahamak|}

Thanks!
Comment
Watch Question

CERTIFIED EXPERT
Top Expert 2016

Commented:
You can have duplicate values in a Map. Subsequent identical values will replace the previous
CERTIFIED EXPERT
Top Expert 2016

Commented:
Sorry. that should have said KEYS not values. If you need that functionality, you'll need to have a List of values for one key
Awarded 2011
Awarded 2011

Commented:
Because it triggers events indpendently - you need
to check where your webProfile closes and it goes to another WebProfile
- it is a bit tricky

Author

Commented:
But line
            System.out.println(url+"<--addidng this url, and keywords ---> "+keywords);
           is always printing correct keys and values (url and keywords). So why hMap.put(url,keywords); which is the line below does not work correctly? End result is: {http://www.hosthelp.pl=kulka|bananahamak|, http://www.wp.pl=kulka|bananahamak|}
insead of
 {http://www.hosthelp.pl=opinie hosting|drugi keyword|, http://www.wp.pl=kulka|bananahamak|}
Awarded 2011
Awarded 2011
Commented:
Unlock this solution with a free trial preview.
(No credit card required)
Get Preview
Awarded 2011
Awarded 2011

Commented:
I'm gueesing that I could not connect because
of local issues;
If you juts change:

filename = ...

to

filename =  "http://webtools.drynski.com:8080/WebTools/keywords/api/user?pass=user";

I think it should work for you


Author

Commented:
It works!, Thanks a lot!!
Unlock the solution to this question.
Thanks for using Experts Exchange.

Please provide your email to receive a free trial preview!

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.