简体   繁体   English

如何在不考虑 Java 中的命名空间名称的情况下从 XML 获取 Node?

[英]How to get Node from XML without considering namespace name in Java?

I am writing a java program in which I am parsing input xml file which looks like this:我正在编写一个 java 程序,其中我正在解析如下所示的输入 xml 文件:

...
<ems:DeterminationRequest>
    <ems:MessageInformation>
       <ns17:MessageID xmlns:ns17="http://www.calheers.ca.gov/EHITSAWSInterfaceCommonSchema">1000225404</ns17:MessageID>
       <ns17:MessageTimeStamp xmlns:ns17="http://www.calheers.ca.gov/EHITSAWSInterfaceCommonSchema">2015-07-28T01:17:04</ns17:MessageTimeStamp>
       <ns17:SendingSystem xmlns:ns17="http://www.calheers.ca.gov/EHITSAWSInterfaceCommonSchema">CH</ns17:SendingSystem>
       <ns17:ReceivingSystem xmlns:ns17="http://www.calheers.ca.gov/EHITSAWSInterfaceCommonSchema">LD</ns17:ReceivingSystem>
       <ns17:ServicingFipsCountyCode xmlns:ns17="http://www.calheers.ca.gov/EHITSAWSInterfaceCommonSchema">037</ns17:ServicingFipsCountyCode>
    </ems:MessageInformation>
</ems:DeterminationRequest>
...

Now I am trying to get node "ems:MessageInformation" without considering namespace name "ems".现在我试图在不考虑名称空间名称“ems”的情况下获取节点“ems:MessageInformation”。 So I tried following lines of code:所以我尝试了以下几行代码:

Document doc = db.parse(new FileInputStream(new File("D:\\test.xml")));
Node element = doc.getDocumentElement().getElementsByTagNameNS("*","MessageInformation").item(0);
System.out.println(element.getNodeName());

But it's giving Null Pointer exception because function is not reading required node.但它给出了空指针异常,因为函数没有读取所需的节点。 I gone through this link for reference.我浏览了这个链接以供参考。 Can someone tell me what I am doing wrong here?有人可以告诉我我在这里做错了什么吗?

This is an odd/buggy behaviour in den NodeList implementation returned by这是由返回的 den NodeList实现中的奇怪/错误行为

doc.getDocumentElement().getElementsByTagNameNS("*","MessageInformation")

It allows you to access item(0) but returns a null object.它允许您访问item(0)返回一个空对象。 (If you are using a current JDK the NodeList implementation is com.sun.org.apache.xerces.internal.dom.DeepNodeListImpl which lazily loads its items and shows this buggy behaviour). (如果您使用的是当前的 JDK,则NodeList实现是com.sun.org.apache.xerces.internal.dom.DeepNodeListImpl ,它会延迟加载其项目并显示此错误行为)。


To prevent the NullPointerException you should first check if the returned NodeList has a length > 0:为了防止NullPointerException您应该首先检查返回的NodeList的长度是否大于 0:

NodeList result = doc.getDocumentElement().getElementsByTagNameNS("*","MessageInformation");
if (result.getLength() > 0) {
    Node element = (Element)result.item(0);
    ...
}

Then you need to find out why getElementsByTagNameNS does not return the element.然后你需要找出为什么getElementsByTagNameNS不返回元素。

One possible reason could be that you parsed the document without namespace support.一种可能的原因可能是您在没有命名空间支持的情况下解析了文档。 The consequence is that the dom elements don't have namespace information and getElementsByTagNameNS fails.结果是 dom 元素没有命名空间信息并且getElementsByTagNameNS失败。

To turn on namespace support use:要打开命名空间支持,请使用:

DocumentBuilderFactory.setNamespaceAware(true);

Alternatively without namespace support you could search for或者没有命名空间支持,您可以搜索

NodeList nl = doc.getDocumentElement().getElementsByTagName("ems:MessageInformation");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM