Having problem in retrieving data from res/xml/books.xml in android
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);
精彩评论