解析xml时如何检查空标签?

时间:2021-09-09 07:18:58

I am using the Document object to extract all the tags from an xml. If the xml has an empty tag, I get a null pointer exception. How do I guard against this? How do I check for an empty tag?

我正在使用Document对象从xml中提取所有标记。如果xml有一个空标记,我会得到一个空指针异常。我该如何防范这个?如何检查空标签?

<USTrade>
<CreditorId>
<CustomerNumber>xxxx</CustomerNumber>
<Name></Name>
<Industry code="FY" description="Factor"/>
</CreditorId>
<DateReported format="MM/CCYY">02/2012</DateReported>
<AccountNumber>54000</AccountNumber>
<HighCreditAmount>0000299</HighCreditAmount>
<BalanceAmount>0000069</BalanceAmount>
<PastDueAmount>0000069</PastDueAmount>
<PortfolioType code="O" description="Open Account (30, 60, or 90 day account)"/>
<Status code="5" description="120 Dys or More PDue"/>
 <Narratives>
<Narrative code="GS" description="Medical"/>
<Narrative code="CZ" description="Collection Account"/>
</Narratives>
</USTrade>
<USTrade>

So, when I use:

所以,当我使用时:

                NodeList nm = docElement.getElementsByTagName("Name");
                if (nm.getLength() > 0)
                    name = nullIfBlank(((Element) nm.item(0))
                            .getFirstChild().getTextContent());

Nodelist gives a length of 1, because there is a tag, but when I do getTextContent(), it hits the null pointer because FirstChild() doesn't return anything for tag = Name

Nodelist给出的长度为1,因为有一个标签,但是当我执行getTextContent()时,它会命中空指针,因为FirstChild()没有为tag = Name返回任何内容

And, I have done this for each xml tag. Is there a simple check I can do before every tag extraction?

而且,我已经为每个xml标签做了这个。在每次标记提取之前,我都可以进行简单的检查吗?

3 个解决方案

#1


6  

The first thing I would do would be to unchain your calls. This will give you the chance to determine exactly which reference is null and which reference you need to do a null check for:

我要做的第一件事就是取消你的电话。这将使您有机会确定哪个引用确实为null以及您需要对哪个引用进行空检查:

 NodeList nm = docElement.getElementsByTagName("Name");
                if (nm.getLength() > 0) {
                    Node n = nm.item(0);
                    Node child = n.getFirstChild();
                    if(child == null) {
                        // null handling
                        name = null;
                     }
                    else {
                       name = nullIfBlank(child.getTextContent());
                    }

                 }

Also, check out the hasChildNodes() method on Node! http://docs.oracle.com/javase/1.4.2/docs/api/org/w3c/dom/Node.html#hasChildNodes%28%29

另外,请查看Node上的hasChildNodes()方法! http://docs.oracle.com/javase/1.4.2/docs/api/org/w3c/dom/Node.html#hasChildNodes%28%29

#2


0  

while(current != null){
                if(current.getNodeType() == Node.ELEMENT_NODE){
                    String nodeName = current.getNodeName();
                    System.out.println("\tNode: "+nodeName);
                    NamedNodeMap attributes = current.getAttributes();
                    System.out.println("\t\tNumber of Attributes: "+attributes.getLength());
                    for(int i=0; i<attributes.getLength(); i++){
                        Node attr = attributes.item(i);
                        String attName = attr.getNodeName();
                        String attValue= attr.getNodeValue();
                        System.out.println("\t\tAttribute Name: "+ attName+ "\tAttribute Value:"+ attValue);
                    }
                }

Are you also wanting to print out the value of the node? If so, it's one line of code in my example you would have to add, and I can share that as well.

您是否也想打印出节点的值?如果是这样,我必须添加一行代码,我也可以分享。

#3


0  

Did you tried something like that?

你尝试过类似的东西吗?

NodeList nm = docElement.getElementsByTagName("Name");
if ((Element) nm.item(0))
 name = nullIfBlank(((Element) nm.item(0)).getFirstChild().getTextContent());

#1


6  

The first thing I would do would be to unchain your calls. This will give you the chance to determine exactly which reference is null and which reference you need to do a null check for:

我要做的第一件事就是取消你的电话。这将使您有机会确定哪个引用确实为null以及您需要对哪个引用进行空检查:

 NodeList nm = docElement.getElementsByTagName("Name");
                if (nm.getLength() > 0) {
                    Node n = nm.item(0);
                    Node child = n.getFirstChild();
                    if(child == null) {
                        // null handling
                        name = null;
                     }
                    else {
                       name = nullIfBlank(child.getTextContent());
                    }

                 }

Also, check out the hasChildNodes() method on Node! http://docs.oracle.com/javase/1.4.2/docs/api/org/w3c/dom/Node.html#hasChildNodes%28%29

另外,请查看Node上的hasChildNodes()方法! http://docs.oracle.com/javase/1.4.2/docs/api/org/w3c/dom/Node.html#hasChildNodes%28%29

#2


0  

while(current != null){
                if(current.getNodeType() == Node.ELEMENT_NODE){
                    String nodeName = current.getNodeName();
                    System.out.println("\tNode: "+nodeName);
                    NamedNodeMap attributes = current.getAttributes();
                    System.out.println("\t\tNumber of Attributes: "+attributes.getLength());
                    for(int i=0; i<attributes.getLength(); i++){
                        Node attr = attributes.item(i);
                        String attName = attr.getNodeName();
                        String attValue= attr.getNodeValue();
                        System.out.println("\t\tAttribute Name: "+ attName+ "\tAttribute Value:"+ attValue);
                    }
                }

Are you also wanting to print out the value of the node? If so, it's one line of code in my example you would have to add, and I can share that as well.

您是否也想打印出节点的值?如果是这样,我必须添加一行代码,我也可以分享。

#3


0  

Did you tried something like that?

你尝试过类似的东西吗?

NodeList nm = docElement.getElementsByTagName("Name");
if ((Element) nm.item(0))
 name = nullIfBlank(((Element) nm.item(0)).getFirstChild().getTextContent());