Moje XML wygląda this-getChildNodes dając nieoczekiwany rezultat
<collected_objects>
<object flag="complete" id="objId" version="1">
<variable_value variable_id="varId">ValueGoesHere</variable_value>
<reference item_ref="2"/>
</object>
<object comment="objComment" flag="complete" id="objId" version="1">
<reference item_ref="1"/>
</object>
</collected_objects>
ja przetworzeniu go za pomocą poniższego Code
Document dom = parser.getDocument();
NodeList collected_objects = dom.getElementsByTagName("object");
System.out.println("Number of collected objects are " + collected_objects.getLength());
for (int i = 0; i < collected_objects.getLength(); i++) {
Node aNode = collected_objects.item(i);
//get children of "objects"
NodeList refNodes = aNode.getChildNodes();
System.out.println("# of chidren are " + refNodes.getLength());
//print attributes of "objects"
NamedNodeMap attributes = aNode.getAttributes();
for (int a = 0; a < attributes.getLength(); a++) {
Node theAttribute = attributes.item(a);
System.out.println(theAttribute.getNodeName() + "=" + theAttribute.getNodeValue());
}
}
wyprowadza as-
Number of collected objects are 2
# of chidren are 5
flag=complete
id=objId
version=1
# of chidren are 3
comment=objComment
flag=complete
id=objId
version=1
Moje pytanie brzmi, dlaczego "# z chidren are" to odpowiednio 5 i 3? Czy nie powinienem oczekiwać odpowiednio 2 i 1? ponieważ pierwszy obiekt ma „variable_value
” i „reference
”, a drugi obiekt ma tylko „reference
”
Zasadniczo, moim zamiarem jest, aby przetwarzać dzieci „obiektów”.