tags:

views:

539

answers:

2

I'm using an old version of the JRE (1.4) where Node.getTextContents() and Node.setTextContents() are not available. Is there a long way to do these actions still?

Example XML:

<MyEle>4119<MyEle/>

Java:

//myEleNode is a Node found while traversing
String nodeString = myEleNode.getTextContent();
if(nodeString.equals("4119")){//do something}
+2  A: 

The text is a child node of the MyEle element, so you would use something like:

MyEle.getFirstChild().getNodeValue()
DMKing
Note that this is true for DOM that is created by parsing XML, but not necessarily true for any built up DOM tree. You could have your text be split between multiple nodes. In those cases, Node.normalize() will normalize the text into a single node
ykaganovich
A: 

You will have to iterate over the children, check if their type is text (node.getNodeType() == Node.TEXT_NODE) and then get the text value using node.getNodeValue().

Bogdan