来自特定节点的Xpath查询

时间:2012-05-13 11:24:22

标签: java xml xpath nodes

我目前支持的常见查询来自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);

,我需要进行哪些更改才能实现

谢谢!

0 个答案:

没有答案
相关问题