[XML-DEV Mailing List Archive Home] [By Thread] [By Date] [Recent Entries] [Reply To This Message]

RE: skipping the null node values in a DOM Tree


java gettextcontent

>if (!chhh.getFirstChild().getNodeValue().equals(null))

Here you are testing whether the string value of the first child node of chh is equal to null. No string in Java ever equals(null) - String.equals(object) only ever returns true if the object is an instanceof String, and null is not an instanceof anything.

If chhh has no children, such as the <description/> element in your example, there is no first child, and getFirstChild() will return null [1]. Attempting to call getNodeValue() on that null reference will cause a NullPointerException to be thrown, which you are observing.

If you are using Java 1.4, use (chhh.getFirstChild() != null) or (chhh.getChildCount() > 0) before walking down the tree accessing the text (and remember that if your source document has not been normalised or contains comments or processing instructions the text content of the element may be spread across many nodes, and the node value of the first node may not be text content).

If you are using Java5 then calling getTextContent() [2] on chhh hides these complications.

 

Pete

1:  http://java.sun.com/j2se/1.4.2/docs/api/org/w3c/dom/Node.html#getFirstChild() 

2: http://java.sun.com/j2se/1.5.0/docs/api/org/w3c/dom/Node.html#getTextContent()


This email and any files attached are intended for the addressee and may contain information of a confidential nature. If you are not the intended recipient, be aware that this email was sent to you in error and you should not disclose, distribute, print, copy or make other use of this email or its attachments. Such actions, in fact, may be unlawful. In compliance with the various Regulations and Acts, General Dynamics UK Limited reserves the right to monitor (and examine for viruses) all emails and email attachments, both inbound and outbound. Email communications and their attachments may not be secure or error- or virus-free and the company does not accept liability or responsibility for such matters or the consequences thereof. Registered Office: 100 New Bridge Street, London EC4V 6JA. Registered in England and Wales No: 1911653.


PURCHASE STYLUS STUDIO ONLINE TODAY!

Purchasing Stylus Studio from our online shop is Easy, Secure and Value Priced!

Buy Stylus Studio Now

Download The World's Best XML IDE!

Accelerate XML development with our award-winning XML IDE - Download a free trial today!

Don't miss another message! Subscribe to this list today.
Email
First Name
Last Name
Company
Subscribe in XML format
RSS 2.0
Atom 0.3
 

Stylus Studio has published XML-DEV in RSS and ATOM formats, enabling users to easily subcribe to the list from their preferred news reader application.


Stylus Studio Sponsored Links are added links designed to provide related and additional information to the visitors of this website. they were not included by the author in the initial post. To view the content without the Sponsor Links please click here.

Site Map | Privacy Policy | Terms of Use | Trademarks
Free Stylus Studio XML Training:
W3C Member
Stylus Studio® and DataDirect XQuery ™are products from DataDirect Technologies, is a registered trademark of Progress Software Corporation, in the U.S. and other countries. © 2004-2013 All Rights Reserved.