Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

org.w3c.dom.Node with Android version less than 2.2

getTextContent() is not a recognized function. getNodeValue() works fine for strings, but anytime I try to parse a number using getNodeValue(), it returns null!

How can I parse a Long from XML using this class?

like image 660
D-Nice Avatar asked Dec 05 '25 08:12

D-Nice


2 Answers

The root cause of this is that the getTextContent() method is a W3C DOM Level 3 method; see the changes section of the DOM level 3 core spec.

The Node interface has two new attributes, Node.baseURI and Node.textContent. ...

and getTextContent() is the getter for the new attribute.

(Presumably, older versions of Android don't implement the DOM level 3 APIs.)

The behavior of getTextContent() is a bit complicated in the general case; see the spec for the textContext attribute. In the simple case where the target node is an Element with (only) text contents, node.getTextContext() is the same as node.getFirstChild().getNodeValue().

like image 106
Stephen C Avatar answered Dec 07 '25 21:12

Stephen C


You need to navigate down to the text node. For instance, with something like this:

<val>10000</val>

the parsed XML tree has an Element node for the tag which, in turn, has a child Text node for the "10000". A typical idiom is

valNode.getFirstChild().getNodeValue()
like image 24
Ted Hopp Avatar answered Dec 07 '25 21:12

Ted Hopp