Win a copy of The Java Performance Companion this week in the Performance forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Reading NULL xml

 
Alan Blass
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I have an XML which looks like this:



And while I was using to code below to read in "<description/>", I get a null pointer exception because it is not "<description></description>".

How can I allow it to read "<description/>" as ""?

I am calling readTwoTags as readTwoTags("root", "title", "description", myXML);

Thanks a lot.

 
Jesper de Jong
Java Cowboy
Saloon Keeper
Pie
Posts: 15369
40
Android IntelliJ IDE Java Scala Spring
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
In which line do you get the NullPointerException? The stack trace tells you exactly where the exception happens.

I'm guessing it is in line 32:

When you have "<description/>", that tag does not have any child tags, so el.getFirstChild() returns null. When you try to call a method (getNodeValue() in this case) on a reference that is null, you get a NullPointerException.

Put the result of el.getFirstChild() in a variable, and check if it is null before calling getNodeValue() on it.
 
Alan Blass
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks Jesper. It worked.
 
Alan Blass
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another question:

How do I read in the value 3 and 4 under key of duration?:



Pleas help. Thanks.
 
Alan Blass
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Anyone, please?
 
Steve Fahlbusch
Bartender
Posts: 605
7
Mac OS X Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Please be patient - all of the moderators are volunteers and they do an amazing job.

so step back and wait
 
Paul Clapham
Sheriff
Posts: 21144
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Well, those things aren't "under" that "key" at all. At least not in the normal sense where you think of an XML document as a tree structure -- they are in the next branch of the tree. So it's best not to ask the question that way, even if in that text representation of the XML they do appear to be "under" the <key> element.

If you're writing code which navigates through that document via a tree structure (like a DOM for example), and you're looking at a <key> element, then go up to its parent, find the <value> element which is its child, and then find its child which will be a text node.
 
Alan Blass
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi!

I have tried:



But I can only print out "key duration". The next sibiling value: is null.

Please help. Thanks
 
William Brogden
Author and all-around good cowpoke
Rancher
Posts: 13064
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Looks to me like the nextSibling() is NOT the "value" element but a Node of type Text containing non-printing characters.

Bill
 
g tsuji
Ranch Hand
Posts: 667
3
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
You sure can try to understand the reason why it is done that way, in particular by displaying the getNodeType() of each nextSibiling [sic] during the looping.
 
Alan Blass
Ranch Hand
Posts: 158
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Thanks! It works.
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic