This week's book giveaway is in the Clojure forum.
We're giving away four copies of Clojure in Action and have Amit Rathore and Francis Avila on-line!
See this thread for details.
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

XML parsing

 
Bharat Chatla
Greenhorn
Posts: 14
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
Ulf Dittmer
Rancher
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
  • 0
  • Mark post as helpful
  • send pies
  • 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
Pie
Posts: 42966
73
  • 0
  • Mark post as helpful
  • send pies
  • 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.
 
I agree. Here's the link: http://aspose.com/file-tools
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic