java - Java 中带有命名空间的 XPath

标签 java xpath namespaces

我想获取标签之间的所有内容,但由于 urn: 命名空间,我不知道该怎么做。

<urn:ResponseStatus version="1.0" xmlns:urn="urn:camera-org">

<urn:requestURL>/CAMERA/Streaming/status</urn:requestURL>
<urn:statusCode>4</urn:statusCode>
<urn:statusString>Invalid Operation</urn:statusString>
<urn:id>0</urn:id>

</urn:ResponseStatus>

有什么想法吗?

最佳答案

  1. 简短回答:使用 XPath local-name()。像这样:xPathFactory.newXPath().compile("//*[local-name()='requestURL']/text()"); 将返回 /CAMERA/Streaming/状态
  2. 或者您可以实现一个映射命名空间名称和 URI 的 NamespaceContext,并在查询之前将其设置在 XPath 对象上。
  3. 看看这个blog article更新:文章已下,可以在webarchive上看到

解决方案 1 示例:

XPath xpath = XPathFactory.newInstance().newXPath();
String responseStatus = xpath.evaluate("//*[local-name()='ResponseStatus']/text()", document);
System.out.println("-> " + responseStatus);

解决方案 2 示例:

// load the Document
Document document = ...;
NamespaceContext ctx = new NamespaceContext() {
    public String getNamespaceURI(String prefix) {
        return prefix.equals("urn") ? "urn:camera-org" : null; 
    }
    public Iterator getPrefixes(String val) {
        return null;
    }
    public String getPrefix(String uri) {
        return null;
    }
};
XPath xpath = XPathFactory.newInstance().newXPath();
xpath.setNamespaceContext(ctx);
String responseStatus = xpath.evaluate("//urn:ResponseStatus/text()", document);
System.out.println("-> " + responseStatus);

编辑

这是一个完整的例子,它正确地获取了元素:

String xml = "<urn:ResponseStatus version=\"1.0\" xmlns:urn=\"urn:camera-org\">\r\n" + //
        "\r\n" + //
        "<urn:requestURL>/CAMERA/Streaming/status</urn:requestURL>\r\n" + //
        "<urn:statusCode>4</urn:statusCode>\r\n" + //
        "<urn:statusString>Invalid Operation</urn:statusString>\r\n" + //
        "<urn:id>0</urn:id>\r\n" + //
        "\r\n" + //
        "</urn:ResponseStatus>";
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
DocumentBuilder builder = factory.newDocumentBuilder();
Document doc = builder.parse(new java.io.ByteArrayInputStream(xml.getBytes()));
XPath xpath = XPathFactory.newInstance().newXPath();
xpath.setNamespaceContext(new NamespaceContext() {
    public String getNamespaceURI(String prefix) {
        return prefix.equals("urn") ? "urn:camera-org" : null;
    }

    public Iterator<?> getPrefixes(String val) {
        return null;
    }

    public String getPrefix(String uri) {
        return null;
    }
});
XPathExpression expr = xpath.compile("//urn:ResponseStatus");
Object result = expr.evaluate(doc, XPathConstants.NODESET);
NodeList nodes = (NodeList) result;
for (int i = 0; i < nodes.getLength(); i++) {
    Node currentItem = nodes.item(i);
    System.out.println("found node -> " + currentItem.getLocalName() + " (namespace: " + currentItem.getNamespaceURI() + ")");
}

关于java - Java 中带有命名空间的 XPath,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13702637/

相关文章:

xml - XPATH:preceding::and ancestor::之间的区别?

php - 作为库分发时如何避免PHP类名冲突?

c# - 系统地替换命名空间

java - 关于 ArrayList 条件赋值的范围问题

java - 当锁持有非最终对象时,该对象的引用是否仍然可以被另一个线程更改?

python - 如何使用 Selenium 和 Python 单击选项卡元素

c# - xslt 3.0 和 xpath3.1 的数组和映射示例

Java:如何打印找到的字符串的下一行

java - 在每个菜单项的标签之前,有一些颜色的空间,我想删除颜色

namespaces - TFS 2010 - TF215097。无法创建未知类型“{clr-namespace :”