Sunday, 11 January 2015

How to get a data value between node tags



Relatively new to XPath, but really need a help and appreciate your hand.


I have following XML file piece:



<div id="current">

<b> Current Data Ratio:</b>
17.35
<span class="neg">-0.23 (-0.84%)</span>
<div id="timestamp">4:29 pm EST, Fri Jan 9</div>
</div>


I want to get data value 17.35. My XPath code below:



Node external_node = (Node) xPath.evaluate("//div[@id='current']", xmlDocument, XPathConstants.NODE);
if(null != external_node) {
NodeList nodeList = external_node.getChildNodes();
for (int i = 0; null!=nodeList && i < nodeList.getLength(); i++) {
Node internal_node = nodeList.item(i);
if(internal_node.getNodeType() == Node.ELEMENT_NODE)
System.out.println(nodeList.item(i).getNodeName() + " : " + internal_node.getFirstChild().getNodeValue());

}
}


then what I get is:



b : Current Data Ratio:

span : -0.23 (-0.84%)

div : 4:29 pm EST, Fri Jan 9


the data 17.35 cannot be obtained. I changed Node type to Node.TEXT_NODE like this:


if(internal_nod.getNodeType() == Node.TEXT_NODE


then I get nothing returned. Then I tried to use text() function, it seems no help either. Any help needed.


No comments:

Post a Comment