【问题标题】:How to parse XML file with JxPath and DOM parser如何使用 JxPath 和 DOM 解析器解析 XML 文件
【发布时间】:2017-02-17 14:31:13
【问题描述】:

我需要一个简单的例子,如何使用 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,但是 我将不胜感激一些帮助或具有特定工作示例的网络资源。

【问题讨论】:

    标签: java xml parsing dom jxpath


    【解决方案1】:

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

    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
    }
    

    【讨论】:

      【解决方案2】:

      您确定 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 来访问文档。

      【讨论】:

        【解决方案3】:

        是的,我从几个小时开始阅读 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();
                }
        
            }
        

        【讨论】:

          猜你喜欢
          • 2013-12-24
          • 2013-01-31
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-07-18
          • 1970-01-01
          • 1970-01-01
          • 2011-12-15
          相关资源
          最近更新 更多