带有Java命名空间的XPath [英] XPath with namespace in Java

查看:126
本文介绍了带有Java命名空间的XPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获取标签之间的所有内容,但由于urn:namespace,我不知道如何做到这一点。

I would like to get all the content in between the tags but I do not know how to do this because of the urn: namespace.

<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 / status

  2. 或者您可以实现映射名称空间的 NamespaceContext 在查询之前将名称和URI设置在XPath对象上。

  3. 看看这个博客文章更新:文章已关闭,您可以在 webarchive

  1. Short answer: use XPath local-name(). Like this: xPathFactory.newXPath().compile("//*[local-name()='requestURL']/text()"); will return /CAMERA/Streaming/status
  2. Or you can implement a NamespaceContext that maps namespaces names and URIs and set it on the XPath object before querying.
  3. Take a look at this blog article, Update: the article is down, you can see it on 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);

修改

这是一个完整的例子,它正确检索元素:

This is a complete example, it correctly retrieve the element:

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命名空间的XPath的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆