I have an XML document:
<entities xmlns="urn:yahoo:cap"> <entity score="0.988"> <text end="4" endchar="4" start="0" startchar="0">Messi</text> <wiki_url>http://en.wikipedia.com/wiki/Lionel_Messi</wiki_url> <types> <type region="us">/person</type> </types> </entity> </entities>
I have a TreeMap<String,String> data that stores getTextContent() for the "text" and "wiki_url" . Some "entity" will only have a "text" element (no "wiki_url" ), so I need to find out when there is only a text element as a child and when there is a "wiki_url" . I could use document.getElementByTag("text") and document.getElementByTag("wiki_url") , but then I would lose the connection between the text and the URL.
I am trying to get the number of elements in an "entity" element using:
NodeList entities = document.getElementsByTagName("entity"); //List of all the entity nodes int nchild; //Number of children System.out.println("Number of entities: "+ entities.getLength()); //Prints 1 as expected nchild=entities.item(0).getChildNodes().getLength(); //Returns 7
However, as shown above, this returns 7 (which I donβt understand, of course, its 3 or 4 if you included the grandson) Then I was going to use the number of children to scroll through them all to check if getNodeName().equals("wiki_url") and save it to the data, if correct.
Why do I get the number of children as 7 when I can only count 3 children and 1 grandson?
source share