繁体   English   中英

Java中的XML XPath解析

[英]XML XPath Parsing in Java

这是以下标准代码,用于使用Java中的XPath解析XML。 我无法调试为什么我得到空值。 我已经附加了java文件,xml文件和输出。 如果有人可以解释我哪里出错了,将不胜感激。 提前致谢! :)

XPathParser.java

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpression;
import javax.xml.xpath.XPathFactory;

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;

public class XPathParser {
    public static void main(String args[]) throws Exception {
        //loading the XML document from a file
        DocumentBuilderFactory builderfactory = DocumentBuilderFactory.newInstance();
        builderfactory.setNamespaceAware(true);

        //XML read
        DocumentBuilder builder = builderfactory.newDocumentBuilder();
        Document xmlDocument = builder.parse("Stocks.xml");

        // Creates a XPath factory
        XPathFactory factory = javax.xml.xpath.XPathFactory.newInstance();

        //Creates a XPath Object
        XPath xPath = factory.newXPath();

        //Compiles the XPath expression
        //XPathExpression xPathExpression_count = xPath.compile("count(//stock)");
        XPathExpression xPathExpression = xPath.compile("//stock");

        //Run the query and get a nodeset
        Object result = xPathExpression.evaluate(xmlDocument,XPathConstants.NODESET);

        //Cast the result into a DOM nodelist
        NodeList nodes = (NodeList) result;
        System.out.println(nodes.getLength());
        System.out.println(nodes.item(0));
        for (int i=0; i<nodes.getLength();i++){
          System.out.println(nodes.item(i).getNodeValue());
        }
    }
}

Stocks.xml

<?xml version="1.0" encoding="UTF-8"?>
<stocks>
       <stock>
              <symbol>ABC</symbol>
              <price>10</price>
              <quantity>50</quantity>
       </stock>
       <stock>
              <symbol>XYZ</symbol>
              <price>20</price>
              <quantity>1000</quantity>
       </stock>
</stocks>

输出:

2
[stock: null]
null
null

您尝试在Stock节点上调用getNodeValue方法-这没有意义,因为它们没有值,它们是父节点。

您可以遍历Stock的子节点并查找信息:

final Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(xml.getBytes()));
final XPathExpression expression = XPathFactory.newInstance().newXPath().compile("//stock");
final NodeList nodeList = (NodeList) expression.evaluate(document, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); ++i) {
    final NodeList childList = ((Element) nodeList.item(i)).getChildNodes();
    for (int j = 0; j < childList.getLength(); ++j) {
        final Node node = childList.item(j);
        if (node.getNodeType() == Node.ELEMENT_NODE) {
            System.out.println(node.getNodeName() + "=" + node.getTextContent());
        }
    }
}

输出:

symbol=ABC
price=10
quantity=50
symbol=XYZ
price=20
quantity=1000

请注意,您必须按类型过滤子Node ,否则将遍历子节点和该节点的文本值(作为文本节点出现)的组合。 这是以这种方式遍历XML的常见陷阱。

您还可以遍历Stock所有子文本节点:

final Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(xml.getBytes()));
final XPathExpression expression = XPathFactory.newInstance().newXPath().compile("//stock/*/text()");
final NodeList nodeList = (NodeList) expression.evaluate(document, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); ++i) {
    final Node node = nodeList.item(i);
    System.out.println(node.getNodeValue());
}

输出:

ABC
10
50
XYZ
20
1000

在这种情况下,您将遍历Stock子级的子级的所有文本节点-这意味着您会丢失有关节点名的信息。 但是您可以通过遍历不是文本节点的所有Stock子级来重新创建第一种方法:

final Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(xml.getBytes()));
final XPathExpression expression = XPathFactory.newInstance().newXPath().compile("//stock/*");
final NodeList nodeList = (NodeList) expression.evaluate(document, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); ++i) {
    final Node node = nodeList.item(i);
    System.out.println(node.getNodeName() + "=" + node.getTextContent());
}

输出:

symbol=ABC
price=10
quantity=50
symbol=XYZ
price=20
quantity=1000

或者,如果需要更具体的信息,则可以在XPath中选择一个特定的子节点:

final Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(new ByteArrayInputStream(xml.getBytes()));
final XPathExpression expression = XPathFactory.newInstance().newXPath().compile("//stock/symbol/text()");
final NodeList nodeList = (NodeList) expression.evaluate(document, XPathConstants.NODESET);
for (int i = 0; i < nodeList.getLength(); ++i) {
    final Node node = nodeList.item(i);
    System.out.println(node.getNodeValue());
}

输出:

ABC
XYZ

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM