本文介紹了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("http://*[local-name()='requestURL']/text()");
將返回/CAMERA/Streaming/狀態
- 或者您可以實現一個
NamespaceContext
來映射命名空間名稱和 URI,并在查詢之前將其設置在 XPath 對象上. - 看看這篇博客文章,更新:文章已下架,您可以在webarchive
- Short answer: use XPath
local-name()
. Like this:xPathFactory.newXPath().compile("http://*[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("http://*[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("http://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("http://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的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!