How to check for empty tags when parsing xml?

I am using a Document object to retrieve all tags from xml. If xml has an empty tag, I get an exception with a null pointer. How can I protect myself from this? How to check an empty tag?

<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 get getTextContent (), it hits the null pointer because FirstChild () does not return anything for tag = Name

And I did it for every xml tag. Is there a simple check I can do before each tag extraction?

+3
source share
3 answers

, , . , :

 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());
                    }

                 }

hasChildNodes() Node! http://docs.oracle.com/javase/1.4.2/docs/api/org/w3c/dom/Node.html#hasChildNodes%28%29

+6
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);
                    }
                }

node? , , , .

0

Have you tried something like this?

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

All Articles