1

我需要一个简单的例子,如何使用 Java DOM 解析器和 Apache JxPath 解析 XML 文件。我知道 DOM 解析器技术,但现在我正在尝试将 JxPath 集成到我的源代码中。

我在网络中搜索过,但找不到工作示例。

对于测试,我有这个 xml:

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
  <cd gender="male">
    <title>Empire Burlesque</title>
    <artist>Bob Dylan</artist>
    <country>USA</country>
    <company>Columbia</company>
    <price>10.90</price>
    <year>1985</year>
  </cd>
  <cd gender="male">
    <title>Hide your heart</title>
    <artist>Bonnie Tyler</artist>
    <country>UK</country>
    <company>CBS Records</company>
    <price>9.90</price>
    <year>1988</year>
  </cd>
</catalog>

Java代码:

File file = new File("Files/catalog.xml");
DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
try
{
    DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
    Document doc = docBuilder.parse(file);

    NodeList nList = doc.getElementsByTagName("cd");
    for(int j = 0; j<nList.getLength(); j++){

        Element element = (Element)nList.item(j);
        System.out.println(element.getElementsByTagName("artist").item(0).getTextContent() + "\n");

    }

}
catch (ParserConfigurationException | SAXException | IOException e)
{
    e.printStackTrace();
}

我已经阅读了类Container、DocumentContainer 和 JXPathContext,但我将不胜感激一些帮助或具有特定工作示例的 Web 源。

4

3 回答 3

3

这是与您进行类似工作的示例。

File file = new File("Files/catalog.xml");
DocumentContainer dc = new DocumentContainer(file.toURI().toURL());
JXPathContext ctx = JXPathContext.newContext(dc);
Iterator iter = ctx.iterate("//cd/artist");
//In this case, following API will return DOM object
//Iterator iter = ctx.selectNodes("//cd/artist").iterator();
while (iter.hasNext()) {
    System.out.println(iter.next());//object type is String
}
于 2017-02-17T16:45:40.437 回答
1

您确定 JXPath 是您需要的吗?JXPath 是一个 Apache 库,用于在不一定是 XML 的事物上使用 XPath 表达式,例如 Java 对象树或映射。如果您已经从 XML 创建了一个 DOM,那么您也可以在其上使用默认的 Java XPath 实现。请参见此处:https ://docs.oracle.com/javase/7/docs/api/javax/xml/xpath/XPathFactory.html 。事实上,JXpath 在这里对你不利,因为 DOM 有一个对象树,像元素名称这样的元数据ElementText所以不是得到像//cd/artist你这样的表达式,而是得到像//*[@tagName='cd']/childNodes[@tagName='artist'].

现在,如果您出于某种原因必须能够在可能是 Java 对象树或 DOM 的东西上使用 XPath 表达式,而无需预先知道它,在这种情况下,JXPath 将是一个很好的用例beckyang 在他们的回答中描述的方式:通过使用 JXPath DocumentContainer 来访问文档。

于 2017-02-17T16:59:28.523 回答
0

是的,我从几个小时开始阅读 JxPath,现在我理解了这个 API 的逻辑

非常感谢您的回答。这是我的一些代码

public class Main {
    private final static Logger log = Logger.getLogger(Main.class);

    public static void main(String[] args) {

        PropertyConfigurator.configure("log4j.properties");

        File file = new File("students.xml");
        DocumentBuilderFactory docFactory = DocumentBuilderFactory.newInstance();
        try
        {
            DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
            Document doc = docBuilder.parse(file);

            NodeList nList = doc.getElementsByTagName("cd");
            for(int j = 0; j<nList.getLength(); j++){

                Element element = (Element)nList.item(j);

               JXPathContext context = JXPathContext.newContext(element);
               log.debug(context.getValue("company/address/city[2]"));

            }

        }
        catch (ParserConfigurationException | SAXException | IOException e)
        {
            e.printStackTrace();
        }

    }
于 2017-02-17T17:10:43.777 回答