I want to execute an xpath query
i added
import javax.xml.xpath.*;
and i used this code
XPath xpath = XPathFactory.newInstance().newXPath();
String expression = "/books/book";
InputSource inputSource = new InputSource("books.xml");
NodeSet nodes =(NodeSet) xpath.evaluate(expression, inputSource, XPathConstants.NODESET);
I used this documentation
but an error NodeSet cannot be resolved to a type
is displayed
Thank you in advance
Update
I chaged the code to:
XPathFactory xPathFactory = XPathFactory.newInstance();
// To get an instance of the XPathFactory object itself.
XPath xPath = xPathFactory.newXPath();
// Create an instance of XPath from the factory class.
String expression = "/books/book";
XPathExpression xPathExpression = xPath.compile(expression);
// Compile the expression to get a XPathExpression object.
Object result = xPathExpression.evaluate(R.raw.books);
//Evaluate the expression against the XML Document to get the result.
But an error displayed in this lines
XPathExpression xPathExpression = xPath.compile开发者_运维百科(expression);
Object result = xPathExpression.evaluate(R.raw.books);
xPath.compile(expression);
and xPathExpression.evaluate(R.raw.books);
errors
what i do now?
According to this documentation
the java representation of NodeSet
is NodeList
. You can try to replace NodeSet
with NodeList
.
NodeList nodes =(NodeList) xpath.evaluate(expression, inputSource, XPathConstants.NODESET);
精彩评论