aspose file tools*
The moose likes XML and Related Technologies and the fly likes Reading NULL xml Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "Reading NULL xml" Watch "Reading NULL xml" New topic
Author

Reading NULL xml

Alan Blass
Ranch Hand

Joined: Mar 21, 2010
Posts: 119
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

Joined: Aug 16, 2005
Posts: 14274
    
  21

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.


Java Beginners FAQ - JavaRanch SCJP FAQ - The Java Tutorial - Java SE 8 API documentation
Alan Blass
Ranch Hand

Joined: Mar 21, 2010
Posts: 119
Thanks Jesper. It worked.
Alan Blass
Ranch Hand

Joined: Mar 21, 2010
Posts: 119
Another question:

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



Pleas help. Thanks.
Alan Blass
Ranch Hand

Joined: Mar 21, 2010
Posts: 119
Anyone, please?
Steve Fahlbusch
Bartender

Joined: Sep 18, 2000
Posts: 571
    
    7

Please be patient - all of the moderators are volunteers and they do an amazing job.

so step back and wait
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18675
    
    8

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

Joined: Mar 21, 2010
Posts: 119
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

Joined: Mar 22, 2000
Posts: 12806
    
    5
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

Joined: Jan 18, 2011
Posts: 535
    
    3
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

Joined: Mar 21, 2010
Posts: 119
Thanks! It works.
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: Reading NULL xml