0

我目前支持的常见查询来自 root ,意思是:

public Object evaluate(String expression, QName returnType) {...}

现在我想从某个给定的 Node 开始执行 Xpath 查询,例如:

public Object evaluate(String expression, Node source, QName returnType) { ? } 

然后,如果我通常的查询是这样的(这里是一个例子):

//load the document into a DOM Document
DocumentBuilderFactory domFactory = DocumentBuilderFactory.newInstance();
domFactory.setNamespaceAware(true); // never forget this!
DocumentBuilder builder = domFactory.newDocumentBuilder();
Document doc = builder.parse("books.xml");
//create an XPath factory
XPathFactory factory = XPathFactory.newInstance();
//create an XPath Object
XPath xpath = factory.newXPath();

//make the XPath object compile the XPath expression
XPathExpression expr = xpath.compile("/inventory/book[3]/preceding-sibling::book[1]");
//evaluate the XPath expression
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
//print the output
System.out.println("1st option:");
for (int i = 0; i < nodes.getLength(); i++) {
    System.out.println("i: " + i);
    System.out.println("*******");
    System.out.println(nodeToString(nodes.item(i)));
    System.out.println("*******");

为了使上述方法发生这种情况,我需要进行哪些更改(public Object evaluate(String expression, Node source, QName returnType);

谢谢!

4

0 回答 0