开发者

Easy way to get the node value of this example

I have a sample XML (Android platform) and I wanted to know the easiest and most efficient approach to get the node value of the node "e". Please help.

<a>
&开发者_如何学Pythonlt;b>
<c>
<e>data</e>

</c>
</b>
<b>

</b>

</a>


Use XPath.

See - http://developer.android.com/reference/javax/xml/xpath/package-summary.html

The XPath expression to grab the correct element would be:

/a/b/c/e

You could coalesce the resulting node into a String to get the textual value.


I think following code will help you to extract data of "e" node.

 NodeList nodes = doc.getElementsByTagName("b");
 for (int i = 0; i < nodes.getLength(); i++) {
     Element element = (Element) nodes.item(i);
     NodeList nodesimg = element.getElementsByTagName("e");
     for (int j = 0; j < nodesimg.getLength(); j++) {
        Element line = (Element) nodesimg.item(j);
        String value=getCharacterDataFromElement(line);
     }
}
public static String getCharacterDataFromElement(Element e) {
  Node child = e.getFirstChild();
  if (child instanceof CharacterData) {
     CharacterData cd = (CharacterData) child;
     return cd.getData();
  }
  return "?";
}


Implement Pull Parser, example is given here: http://icodesnip.com/snippet/java/android-pull-parser-sample

So by your case, you have to write the condition inside the start tag for "e".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜