Unable to read xml with namespace prefix using DOM parser(无法使用 DOM 解析器读取带有命名空间前缀的 xml)
问题描述
这是输入 XML:
<SOAP-ENV:Envelope xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/">
<SOAP-ENV:Header/>
<SOAP-ENV:Body>
<ns2:SendResponse xmlns:ns2="http://mycompany.com/schema/">
<ns2:SendResult>
<ns2:Token>A00179-02</ns2:Token>
</ns2:SendResult>
</ns2:SendResponse>
</SOAP-ENV:Body>
</SOAP-ENV:Envelope>
这是我用来读取 XML 的代码(变量 xmlString 包含上面的 XML):
This the code that I'm using to read the XML (Variable xmlString contains the XML above):
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setNamespaceAware(true);
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource();
is.setCharacterStream(new StringReader(xmlString));
Document doc = db.parse(is);
System.out.println("Element :" + doc.getElementsByTagName("Token").item(0));
System.out.println("Element :" + doc.getElementsByTagName("ns2:Token").item(0));
输出:
Element :null
Element :[ns2:Token: null]
如果我使用ns2:Token"作为标签名称,我可以读取元素,但我不想在代码中使用前缀,因为我不确定它是否相同或将来改变.有什么方法可以读取 xml 元素而无需在标签名称中硬编码命名空间?
I'm able to read the element if I use "ns2:Token" as the tag name, but I don't want to use the prefix in my code as I'm not sure if it'll be the same or change in the future. Is there any way to read the xml element without hard-coding the namespace in the tag name?
推荐答案
W3C dom 命名空间元素的方法:
The W3C dom method for namespaced elements:
getElementsByTagNameNS
NodeList getElementsByTagNameNS(String namespaceURI,
String localName)
Returns a NodeList of all the Elements with a given local name and namespace URI in document order.
Parameters:
namespaceURI - The namespace URI of the elements to match on. The special value "*" matches all namespaces.
localName - The local name of the elements to match on. The special value "*" matches all local names.
Returns:
A new NodeList object containing all the matched Elements.
Since:
DOM Level 2
IIRC 早期版本的 W3C DOM 对命名空间的支持很差,所以我不使用它.但是,如果您将上述内容与完整的 namespaceURI http://schemas.xmlsoap.org/soap/envelope/
一起使用,它应该可以工作.前缀并不重要——它在使用它的文档之外没有永久性.
IIRC earlier version of the W3C DOM had poor support for namespaces so I don't use it. However if you use the above with the full namespaceURI http://schemas.xmlsoap.org/soap/envelope/
it should work. The prefix is unimportant - it has no permanency outside the document it is used in.
那就试试吧:
System.out.println("Element :" + doc.getElementsByTagNameNS(
"http://schemas.xmlsoap.org/soap/envelope/", "Token").item(0));
这篇关于无法使用 DOM 解析器读取带有命名空间前缀的 xml的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:无法使用 DOM 解析器读取带有命名空间前缀的 x


基础教程推荐
- Spring Boot Freemarker从2.2.0升级失败 2022-01-01
- 在螺旋中写一个字符串 2022-01-01
- Java 中保存最后 N 个元素的大小受限队列 2022-01-01
- 如何使用 Stream 在集合中拆分奇数和偶数以及两者的总和 2022-01-01
- 如何在不安装整个 WTP 包的情况下将 Tomcat 8 添加到 Eclipse Kepler 2022-01-01
- 如何对 HashSet 进行排序? 2022-01-01
- 如何使用 Eclipse 检查调试符号状态? 2022-01-01
- 首次使用 Hadoop,MapReduce Job 不运行 Reduce Phase 2022-01-01
- 如何强制对超级方法进行多态调用? 2022-01-01
- 由于对所需库 rt.jar 的限制,对类的访问限制? 2022-01-01