Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 404
  • Last Modified:

parsing xml to hashmap - weird

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!
0
static86
Asked:
static86
  • 3
  • 2
  • 2
1 Solution
 
CEHJCommented:
You can have duplicate values in a Map. Subsequent identical values will replace the previous
0
 
CEHJCommented:
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
0
 
for_yanCommented:
Because it triggers events indpendently - you need
to check where your webProfile closes and it goes to another WebProfile
- it is a bit tricky
0
Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

 
static86Author 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|}
0
 
for_yanCommented:

This works correctly,
only I could not connect with the Internet for some
reason from the program - I read the same stuff
friom the local file

    import org.xml.sax.helpers.DefaultHandler;
    import org.xml.sax.Attributes;
    import org.xml.sax.SAXException;

    import javax.xml.parsers.SAXParserFactory;
    import javax.xml.parsers.SAXParser;

/*
<product>
 <name>laptop</name>
 <price>120</price>
 <maker>Dell</maker>
</product>

*/
    import java.util.ArrayList;
    import java.util.HashMap;


    public class ParsingXMLWithSAX {


       HashMap m = new HashMap();
        ArrayList ar = null;
        String key;

        static String filename = "c:\\temp\\test\\xmlfile.txt" ;

        public  void getElementFromXML() {
            try {
                SAXParserFactory factory = SAXParserFactory.newInstance();
                SAXParser saxParser = factory.newSAXParser();
                DefaultHandler handler = new DefaultHandler() {
                    boolean title = false;
                    boolean length1 = false;
                    boolean actor = false;
                    boolean dvd = false;
                    boolean products = false;
                    String dvdAttValue;
                    String dvdAtt;


                    public void startElement(String uri, String localName,
                            String qName, Attributes attributes)
                            throws SAXException {

                        if (qName.toUpperCase().equalsIgnoreCase("WebProfile")) {
                            title = true;
                            ar = new ArrayList();
                        }

                        if (qName.toUpperCase().equalsIgnoreCase("WebUrl")) {
                            length1 = true;
                        }
                        if (qName.toUpperCase().equalsIgnoreCase("keyWord")) {
                            actor= true;
                        }



                        }
                    public void endElement(String uri, String localName,
                            String qName) throws SAXException {
                        if (qName.toUpperCase().equalsIgnoreCase("WebProfile")) {
                            title = false;
                        }

                        if (qName.toUpperCase().equalsIgnoreCase("WebUrl")) {
                            length1 = false;

                        }
                        if (qName.toUpperCase().equalsIgnoreCase("keyWord")) {
                            actor = false;
                            String s="";
                            for(int j=0; j<ar.size(); j++){
                                s += "|" + ar.get(j);
                                m.put(key,s);

                            }
                        }


                    }
                    public void characters(char ch[], int start, int length)
                            throws SAXException {


                       // if (title && dvd) {
                         //      System.out.println("<title>" + new String(ch, start, length) + "</title>");
                      //  }
                       if (length1) {
                               key= new String(ch, start, length);
                        }
                       if (actor) {
                           ar.add( new String(ch, start, length));
                               System.out.println("<actor> " + new String(ch, start, length) + "</actor>");
                        }


                        }


                };
                saxParser.parse(filename, handler);

            }
            catch (Exception e) {
                e.printStackTrace();
            }



             System.out.println(m);
            
        }

        public static void main(String args[])
        {
            new ParsingXMLWithSAX().getElementFromXML();

        }


    }

Open in new window



output:
{http://www.hosthelp.pl=|opinie hosting|drugi keyword, http://www.wp.pl=|kulka|bananahamak}

Open in new window


input:

<user>
- <webProfile>
  <webUrl>http://www.hosthelp.pl</webUrl> 
  <keyWord>opinie hosting</keyWord> 
  <keyWord>drugi keyword</keyWord> 
  </webProfile>
- <webProfile>
  <webUrl>http://www.wp.pl</webUrl> 
  <keyWord>kulka</keyWord> 
  <keyWord>bananahamak</keyWord> 
  </webProfile>
  </user>

Open in new window

0
 
for_yanCommented:
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


0
 
static86Author Commented:
It works!, Thanks a lot!!
0

Featured Post

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

  • 3
  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now