• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

XML parsing

 
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
Bharat Chatla
Greenhorn
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Posts: 14
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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
Rancher
Posts: 43081
77
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
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.
 
reply
    Bookmark Topic Watch Topic
  • New Topic