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!