使用Java的XPath循环遍历节点并提取特定的子节点值

Boo*_*aka 5 java xpath

我从谷歌了解到,使用XPath从XML中提取数据比使用DOM循环更有意义.

目前,我已经使用DOM实现了一个解决方案,但是代码很冗长,感觉不整洁且不可维护,所以我想切换到更清洁的XPath解决方案.

假设我有这样的结构:

<products>
    <product>
        <title>Some title 1</title>
        <image>Some image 1</image>
    </product>
    <product>
        <title>Some title 2</title>
        <image>Some image 2</image>
    </product>
    ...
</products>
Run Code Online (Sandbox Code Playgroud)

我希望能够为每个<product>元素运行for循环,并在for循环中提取标题和图像节点值.

我的代码看起来像这样:

InputStream is = conn.getInputStream();          
DocumentBuilder builder =
  DocumentBuilderFactory.newInstance().newDocumentBuilder();
Document doc = builder.parse(is);
XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expr = xpath.compile("/products/product");
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList products = (NodeList) result;
for (int i = 0; i < products.getLength(); i++) {
    Node n = products.item(i);
    if (n != null && n.getNodeType() == Node.ELEMENT_NODE) {
        Element product = (Element) n;
        // do some DOM navigation to get the title and image
    }
}
Run Code Online (Sandbox Code Playgroud)

在我的for循环中,我得到每个<product>作为一个Node,投射到一个Element.

我可以简单地用我的实例XPathExpression来编译和运行其他XPathNode还是Element

Gop*_*opi 6

是的,你可以随时这样做 -

XPathFactory factory = XPathFactory.newInstance();
XPath xpath = factory.newXPath();
XPathExpression expr = xpath.compile("/products/product");
Object result = expr.evaluate(doc, XPathConstants.NODESET);
expr = xpath.compile("title"); // The new xpath expression to find 'title' within 'product'.

NodeList products = (NodeList) result;
for (int i = 0; i < products.getLength(); i++) {
    Node n = products.item(i);
    if (n != null && n.getNodeType() == Node.ELEMENT_NODE) {
        Element product = (Element) n;
        NodeList nodes = (NodeList)  expr.evaluate(product,XPathConstants.NODESET); //Find the 'title' in the 'product'
        System.out.println("TITLE: " + nodes.item(0).getTextContent()); // And here is the title 
    }
}    
Run Code Online (Sandbox Code Playgroud)

这里我举了一个提取'title'值的例子.以同样的方式你可以做'图像'