2

I'm new in Java. Want some advice. So, I'm parsing data from Xml file, and adding it to hashMap. Please take a look at a piece of code:

final HashMap<String,String> urls = new HashMap<String,String>(); File products = new File("D:/eclipse/workspace/test/src/main/resources/Products.xml"); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder; dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(products); doc.getDocumentElement().normalize(); NodeList nList = doc.getElementsByTagName("Row"); for (int z=0; z<nList.getLength(); z++) { Node nNode = nList.item(z); Element eElement = (Element) nNode; NodeList a = eElement.getElementsByTagName("item"); for (int i=0; i<a.getLength(); i++) { String b = eElement.getElementsByTagName("item").item(i).getTextContent(); String c = eElement.getElementsByTagName("url").item(i).getTextContent(); urls.put(b, c); System.out.println(urls); } } 

This is my output of hashMap after println in Console:

{Select product=bla-bla-bla} {Single Landmine Shirt=http://www.sample.com/landmine-single-shirt, Select product=bla-bla-bla} {Women's Silver & Black Bar=http://www.sample.com/womens-silver-and-black-bar, Single Landmine Shirt=http://www.sample.com/landmine-single-shirt, Select product=bla-bla-bla} 

As you see, I have progressive set of items with every next iteration of for-cycle :(. But what I really need - is just key(item tag)=value(url tag) pairs in each line. "item" and "url" - are tags from my XML (please see Attached).

I 'd like to have output like this (just one key and one corresponding value):

{Select product=bla-bla-bla} {Single Landmine Shirt=http://www.sample.com/landmine-single-shirt} {Women's Silver & Black Bar=http://www.sample.com/womens-silver-and-black-bar} {High Density Foam Rollers=http://www.sample.com/high-density-foam-rollers} 

How can I update code for getting correct key-value pairs? Will be glad for any answers. Thank you very much! Products.xml

3
  • 1
    1) System.out.println("{" + b + "=" + c + "}"); --- Or 2) Move println() method outside loop. Commented Dec 12, 2015 at 1:06
  • Calling normalize() will not affect result, so it's a waste of CPU cycles. getTextContent() will merge text content, if needed. Commented Dec 12, 2015 at 1:08
  • 1
    @Andreas, thanks. Since I need store values in hashMap and use it further, seems your suggestion number 2) is fit here. I'm appreciate your answer man. Commented Dec 12, 2015 at 22:20

2 Answers 2

2

What you get is normal. I explain it:

  • you populate your Map, by iterating over items
  • and you println this Map, at each step.

Then, as you want just item=url once, I suggest:

Solution 1: dont use your Map for this (keep it if you want to use it after), and just replace

urls.put(b, c); System.out.println(urls); by System.out.println("{"+b+"="+c+"}"); 

Solution 2: keep you Map, but just move your println outside your loop

You will have this once

{Women's Silver & Black Bar=http://www.sample.com/womens-silver-and-black-bar, Single Landmine Shirt=http://www.sample.com/landmine-single-shirt, Select product=bla-bla-bla} 

It depends on your choice of formatting

Sign up to request clarification or add additional context in comments.

2 Comments

so yeah I need to keep Map for sure. And solution number 2 works for me. Big thank! :) I have one lil question here: now it stores in line(String) view as You described in answer, I mean {item=url, item2=url2, etc.} My question: is it possible to store values by pairs in column view? - like this: {item=utl} \n {item2=utl2} \n {item3=utl3} \n etc. ...Thanks!
@EvgEvg, in fact, it is not keep in line, or column, there is an internal structure. If you want to print line, by line, just read how to iterate a Map: stackoverflow.com/questions/46898/…
2

Try this:

final HashMap<String, String> urls = new HashMap<String, String>(); File products = new File("D:/eclipse/workspace/test/src/main/resources/Products.xml"); DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder dBuilder; dBuilder = dbFactory.newDocumentBuilder(); Document doc = dBuilder.parse(products); doc.getDocumentElement().normalize(); NodeList rowsList = doc.getElementsByTagName("Row"); for(int z = 0; z < rowsList.getLength(); z++) { Node rowNode = rowsList.item(z); NodeList rowNodeChildren = rowNode.getChildNodes(); String item = null; String url = null; for(int i = 0; i < rowNodeChildren.getLength(); i++) { Node rowNodeChild = rowNodeChildren.item(i); if("item".equals(rowNodeChild.getNodeName())) { item = rowNodeChild.getTextContent(); } else if("url".equals(rowNodeChild.getNodeName())) { url = rowNodeChild.getTextContent(); } } urls.put(item, url); } 

1 Comment

This is good update for my code, Josh. Thank you! And as I see it from functional side - the result of your code is same as mine. Anyway, what I've got from other answers: the solution - is just moving println outside the loop. Thank you very much again for suggestion.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.