如何在java中为nodelist获取重复元素的子节点

时间:2015-09-30 13:30:39

标签: java dom nodelist

我需要获取name元素的值,该元素是e元素的子元素:

<a>
    <b>
        <c>
            <d>
                <e><name>123</name></e>
                <e><name>456</name></e>
                <e><name>456</name></e>
            </d>
        </c>
    </b>
</a>

这是我的代码:

NodeList lineItemAttributeChildrenList =
    doc.getElementsByTagName("e").item(0).getChildNodes();

if(lineItemAttributeChildrenList != null &&
   lineItemAttributeChildrenList.getLength() > 0) {
    System.out.println("Inside if and checking length" +
                       lineItemAttributeChildrenList.getLength());

    for (int i = 0; i < lineItemAttributeChildrenList.getLength(); i++) {
        System.out.println("i " + i);
        System.out.println("inside for");
        System.out.println("name==============" +
                           lineItemAttributeChildrenList.item(i).getNodeName());
        System.out.println("value==============" +
                           lineItemAttributeChildrenList.item(i).getTextContent());
    }
}

从上面的代码我得到了e元素的第一个内部元素名称值,但对于剩下的2,我无法获得这些值。它不会转到for循环中的第二个e元素。

3 个答案:

答案 0 :(得分:1)

您只访问第一项,这就是您获得1个结果的原因。试试下面。检查eNodes.item(z)中的'z'

    NodeList eNodes = doc.getElementsByTagName("e");
    for (int z = 0; z < eNodes.getLength(); z++) {
        NodeList LineItemAttributeChildrenList = eNodes.item(z).getChildNodes();
        if (LineItemAttributeChildrenList != null && LineItemAttributeChildrenList.getLength() > 0) {
            System.out.println("Inside if and checking length" + LineItemAttributeChildrenList.getLength());
            for (int i = 0; i < LineItemAttributeChildrenList.getLength(); i++) {
                System.out.println("i " + i);

                System.out.println("inside for");
                System.out.println("name==============" + LineItemAttributeChildrenList.item(i).getNodeName());
                System.out.println("value==============" + LineItemAttributeChildrenList.item(i).getTextContent());

            }

        }

    }

您可以直接获取名称节点并对其进行迭代以获取如下所示的值

NodeList nameNodes = doc.getElementsByTagName("name");
for (int i = 0; i < nameNodes.getLength(); i++) {
    String value = nameNodes.item(i).getTextContent();
    System.out.println("value==============" + value);
}

答案 1 :(得分:0)

您将获得“e”元素的NodeList的第0个元素。

    NodeList LineItemAttributeChildrenList = doc.getElementsByTagName("e").item(0).getChildNodes();
...
 for (int i = 0; i < LineItemAttributeChildrenList.getLength(); i++) {

您应该迭代NodeList本身而不是第一个元素的子节点。以下是您的新代码的外观:

    NodeList LineItemAttributeChildrenList = doc.getElementsByTagName("e");
    if (LineItemAttributeChildrenList != null && LineItemAttributeChildrenList.getLength() > 0) 
    {
        System.out.println("Inside if and checking length"+LineItemAttributeChildrenList.getLength());
        for (int i = 0; i < LineItemAttributeChildrenList.getLength(); i++) {
            System.out.println("i "+i);
            System.out.println("inside for");
            System.out.println("name=============="+LineItemAttributeChildrenList.item(i).getNodeName());
            System.out.println("value=============="+LineItemAttributeChildrenList.item(i).getTextContent());
        }
     }

答案 2 :(得分:0)

您正在迭代错误的节点

看看下面的内容:

NodeList LineItemAttributeChildrenList = doc.getElementsByTagName("e"); // Finding all 'e' s
          if (LineItemAttributeChildrenList != null && LineItemAttributeChildrenList.getLength() > 0) {
                  System.out.println("Inside if and checking length"+LineItemAttributeChildrenList.getLength());
                    // Iterate over 'e' s
                    for (int i = 0; i < LineItemAttributeChildrenList.getLength(); i++) {
                        System.out.println("i "+i);
                         Node e = LineItemAttributeChildrenList.item(i); 
                         // Access the 'name' node for this 'e'
                         Node name = e.getChildNodes()[0] // We know that 'e' has only one child.
                             System.out.println("name=============="+name.getNodeName());
                            System.out.println("value=============="+name.getTextContent());


                            }


                    }