Search code examples
javaxmldocument

Can't get XML tag value


I have the following code

protected static final String TAG_numFound                  = "numFound";        //
Document street_xmlDoc = GetXMLDocument(street_url);
Element results = street_xmlDoc.getDocumentElement();
//numFound
Node numFoundNode = results.getElementsByTagName(TAG_numFound).item(0);
String numFound = numFoundNode.getNodeValue() + "(" + numFoundNode.getNodeName() + ")";

and the following XML

<results>
<numFound>1</numFound>
<QTime>66</QTime>
<result>
<distance>0.0</distance>
<name>Agua</name>
</result>
</results>

And in the numFound String variable I'm getting "null(numFound)" I don't understand why I'm not getting "1(numFound)" instead. What I'm doing wrong?


Solution

  • You need to use getTextContent instead of getNodeValue.

    Read this blog post for more info.

    You can see when getNodeValue has any meaning in this javadoc section (or in the below snapshot). node value in XML parsing