问题
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?
回答1:
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()
.
回答2:
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()
回答3:
If as you say "getNodeValue() works fine for strings", then nothing should prevent from doing the:
Long l = Long.getLong(node.getNodeValue());
Note, getNodeValue()
will always return a String
that should be then manually converted to a numerical type.
Also - are you sure you are parsing a right node (the one that holds the needed long value)?
来源:https://stackoverflow.com/questions/4771119/org-w3c-dom-node-with-android-version-less-than-2-2