【问题标题】:Getting all attributes of an xml by using java and Xpath使用 java 和 Xpath 获取 xml 的所有属性
【发布时间】:2019-10-02 11:15:14
【问题描述】:

我有以下 xml:

<?xml version="1.0" encoding="UTF-8"?>
<root xmlns="http://www.test.com/rest/v1" xmlns:xs="http://www.w3.org/2001/XMLSchema">
   <child test="folder" id="2019-05-15-04.52.05.641880A01" />
   <child test="folder" id="2019-05-15-04.52.05.901880A02" />
</root>

我想通过使用 Java 代码和 Xpath 读取上面的 xml,检索子节点的 id(即id="2019-05-15-04.52.05.641880A01" and id="2019-05-15-04.52.05.901880A02")并将它们存储到 List 中。我尝试使用以下 java 代码:

        InputSource source = new InputSource(new StringReader(xml));
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        org.w3c.dom.Document document = db.parse(source);
        XPathFactory xpathFactory = XPathFactory.newInstance();
        XPath xpath = xpathFactory.newXPath();
        return xpath.evaluate(expression, document);

我用下面的 Xpath 和输入的 xml 调用了上面的方法:

*[local-name()='root']/*[local-name()='child']/@id

但我只得到一个 id,而不是所有的 id。关于如何获取所有 id 的任何想法?

【问题讨论】:

  • Java 的内置 DOM 和 XPath 库很糟糕。你需要做xpath.evaluate(expression, document, XPathConstants.NODESET)
  • @kumesana,能否请您告诉我上述 xml 的确切表达方式,以便我处理扩孔的事情?

标签: java xml xslt xpath


【解决方案1】:

我认为您的 Xpath 是正确的。您可以使用以下测试类对其进行验证。

package com.idsk.commons.xsl;

import java.io.IOException;

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

import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class Test {

    public static void main(String[] args) throws ParserConfigurationException, SAXException, IOException, XPathExpressionException {
        DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
        factory.setNamespaceAware(true); // never forget this!
        DocumentBuilder builder = factory.newDocumentBuilder();
        Document doc = builder.parse("D://NewFile.xml");

        // Create XPath
        XPathFactory xpathfactory = XPathFactory.newInstance();
        XPath xpath = xpathfactory.newXPath();

        XPathExpression expr = xpath.compile("*[local-name()='root']/*[local-name()='child']/@id"); 

        Object result = expr.evaluate(doc, XPathConstants.NODESET);
        NodeList nodes = (NodeList) result;

        List<String> ids = new ArrayList<>();
        for (int i = 0; i < nodes.getLength(); i++) {
            System.out.println(nodes.item(i).getNodeValue());
            ids.add(nodes.item(i).getNodeValue()); //store them into List
        }
    }
}

它将创建以下输出:

2019-05-15-04.52.05.641880A01

2019-05-15-04.52.05.901880A02

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2023-03-27
    • 1970-01-01
    • 2021-10-04
    • 1970-01-01
    • 1970-01-01
    • 2019-04-29
    • 1970-01-01
    相关资源
    最近更新 更多