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

。_饼干妹妹 提交于 2019-12-11 10:42:54

问题


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);

来源:https://stackoverflow.com/questions/6102872/having-problem-in-retrieving-data-from-res-xml-books-xml-in-android

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!