XPath with namespace in Java(Java中带有命名空间的XPath)
本文介绍了Java中带有命名空间的XPath的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想获取标签之间的所有内容,但由于 urn: 命名空间,我不知道该怎么做.
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>
有什么想法吗?
推荐答案
- 简答:使用 XPath
local-name()
.像这样:xPathFactory.newXPath().compile("//*[local-name()='requestURL']/text()");
将返回/CAMERA/Streaming/状态
- 或者您可以实现一个
NamespaceContext
来映射命名空间名称和 URI,并在查询之前将其设置在 XPath 对象上. - 看看这篇博客文章,更新:文章已下架,您可以在webarchive
- Short answer: use XPath
local-name()
. Like this:xPathFactory.newXPath().compile("//*[local-name()='requestURL']/text()");
will return/CAMERA/Streaming/status
- Or you can implement a
NamespaceContext
that maps namespaces names and URIs and set it on the XPath object before querying. - 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">
" + //
"
" + //
"<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>";
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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
沃梦达教程
本文标题为:Java中带有命名空间的XPath
基础教程推荐
猜你喜欢
- 无法使用修饰符“public final"访问 java.util.Ha 2022-01-01
- FirebaseListAdapter 不推送聊天应用程序的单个项目 - Firebase-Ui 3.1 2022-01-01
- 在 Libgdx 中处理屏幕的正确方法 2022-01-01
- 降序排序:Java Map 2022-01-01
- Java:带有char数组的println给出乱码 2022-01-01
- “未找到匹配项"使用 matcher 的 group 方法时 2022-01-01
- 设置 bean 时出现 Nullpointerexception 2022-01-01
- 如何使用 Java 创建 X509 证书? 2022-01-01
- 减少 JVM 暂停时间 >1 秒使用 UseConcMarkSweepGC 2022-01-01
- Java Keytool 导入证书后出错,"keytool error: java.io.FileNotFoundException &拒绝访问" 2022-01-01