问题
I need to fetch the value of the name
element which is a child of an e
element:
<a>
<b>
<c>
<d>
<e><name>123</name></e>
<e><name>456</name></e>
<e><name>456</name></e>
</d>
</c>
</b>
</a>
This is my code:
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());
}
}
From the above code I just get the first inner element name value for the e
element, but for the remaining 2 I am not able to get those values. It is not going to the second e
element in the for loop.
回答1:
You are getting 0th element of the NodeList of "e" elements.
NodeList LineItemAttributeChildrenList = doc.getElementsByTagName("e").item(0).getChildNodes();
...
for (int i = 0; i < LineItemAttributeChildrenList.getLength(); i++) {
You should iterate through the NodeList itself instead of the children of the first element. Here is how your new code would look:
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:
You are accessing only first item thats why you are getting 1 result. try below. Check 'z' in eNodes.item(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());
}
}
}
You can directly get name nodes and iterate over it to get values as below
NodeList nameNodes = doc.getElementsByTagName("name");
for (int i = 0; i < nameNodes.getLength(); i++) {
String value = nameNodes.item(i).getTextContent();
System.out.println("value==============" + value);
}
回答3:
You are iterating over wrong Node
Take a look below:
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());
}
}
来源:https://stackoverflow.com/questions/32867452/how-to-fetch-the-child-nodes-of-repeated-elements-in-java-for-nodelist