wood burning stoves 2.0*
The moose likes Java in General and the fly likes XML parsing Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Java in General
Bookmark "XML parsing" Watch "XML parsing" New topic
Author

XML parsing

Bharat Chatla
Greenhorn

Joined: May 19, 2005
Posts: 14
I am trying to parse xml using JDOM and if the tag value is empty it throws null pointer exception even though i check for null value.

code
NodeList description = rootElement.getElementsByTagName("description");
for (int i = 0; i < description.getLength(); i++) {
Element el = (Element)description.item(i);
if(el.getFirstChild().getNodeValue()!=null||!"".equals(el.getFirstChild().getNodeValue()))
{System.out.println("description value"+el.getFirstChild().getNodeValue());}
}


XML:
<programArea>
<byBusinessArea name="Corporate, Investment Banking and Markets">
<description></description>
<country countryName="test">
<program programName="">
<businessArea></businessArea>
<lengthOfProgram></lengthOfProgram>
<briefDesc></briefDesc>
<entryCriteria></entryCriteria>
<nationality></nationality>
<url></url>
</program>
</country>

</byBusinessArea>
</programArea>

please suggest. Also suggest which parser is good one and any sample code for parsing.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41102
    
  45
You're checking "el.getFirstChild().getNodeValue()" for null, but you're not checking 'el.getFirstChild()" for null. And since "description" has no children, it will be null.


Ping & DNS - my free Android networking tools app
Bharat Chatla
Greenhorn

Joined: May 19, 2005
Posts: 14
I have also tries
if(el.getFirstChild()!=null||!"".equals(el.getFirstChild()))
{System.out.println("description value"+el.getNodeValue());}
}
but it prints description value as null even if description has value.
Bharat Chatla
Greenhorn

Joined: May 19, 2005
Posts: 14
If you can give me some links for more on parsing in terms of code then it will be very nice.thanks a lot.
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41102
    
  45
I'm not sure what you were expecting to get by calling el.getNodeValue(), but I'm not surprised that it's null, since "description" is an element.

If you had "<description>something</description>" and then called "el.getFirstChild().getNodeValue()", I think you might be getting somewhere.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: XML parsing
 
Similar Threads
Dom Parser and special character
help with web service to display xml file contents using netbeans
Dom parser for XML file
Using XML in search criteria rather than hard coding it
servlets and xml