开发者

Having problem in retrieving data from res/xml/books.xml in android

开发者_StackOverflow社区 Merged with Need help in reading an xml file.

I am having problem in displaying data from an xml file which is in res/xml/books.xml Below is the code I am working with. It is suppose to work but in this case I cannot get the textview to check the id and then display the description in textview. Please if anyone could review the code and tell me what wrong here. Thanks

<?xml version="1.0" encoding="utf-8" ?>
<Books>
  <Number id ="1">
    <Description>This is science book.
    </Description>
  </Number> 
  <Number id = "2">
    <Description>This is about cooking.
   </Description>
  </Number>
</Books> 

Here's the code but doesn't displays anything in textview like it's not even reading the xml file.

XPath xpath = XPathFactory.newInstance().newXPath();

String expression = "//Number[@id='2']/Description";
InputSource inputSource = new InputSource("res/xml/myxml.xml");
Node node = (Node) xpath.evaluate(expression, inputSource, XPathConstants.NODE);
String description = node.getTextContent();
tv.setText(" " + description);
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜