XML解析后无法获得所需的输出

时间:2015-12-12 00:49:11

标签: java xml xml-parsing hashmap

我是Java新手。 想要一些建议。 所以,我正在解析Xml文件中的数据,并将其添加到hashMap。 请看一段代码:

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);                               
                }
            }

这是我在Console中打印后的hashMap输出:

{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}

正如你所看到的那样,我在每个下一个for-cycle迭代中都有渐进式项目:(但我真正需要的是 - 只是(项目标签) = value <每行中的/ strong>(url标记)对。&#34; item&#34; &#34; url&#34; - 来自我的XML标记(请参见附件)。

我希望得到这样的输出(只有一个键和一个对应的值):

{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}

如何更新代码以获取正确的键值对? 对任何答案都会很高兴。非常感谢你! Products.xml

2 个答案:

答案 0 :(得分:2)

试试这个:

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 :(得分:2)

你得到的是正常的。我解释一下:

  • 通过迭代项目
  • 填充地图
  • 并在每一步打印此地图。​​

然后,如果您只想要item = url一次,我建议:

解决方案1:不要使用你的Map(如果你想在之后使用它,请保留它),然后只需替换

urls.put(b, c);
System.out.println(urls);  

by

 System.out.println("{"+b+"="+c+"}");

解决方案2:保持Map,但只需将println移到循环外

你将拥有这一次

{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}

这取决于您选择的格式