[Javascript] Parse XML: What to do with with empty nodes

liorean liorean at gmail.com
Fri Feb 25 14:53:27 CST 2005


On Fri, 25 Feb 2005 13:25:48 -0700, Alan Gardner <gardner.alan at gmail.com> wrote:
> How do I check to see if a XML node has a value or not?

The value of a node is contained in it's nodeValue property. But
that's not what you were interested in. Content and value are not the
same thing.
 
> I'm using xmlhttp to return an xml document from which I pull data to
> display to the user. Occasionally, it returns a node with no value
> <project-id/> and my script throws a
> "c.getElementsByTagName('project-id')[0].firstChild.data" has no
> properties error.  I've tried checking for empty string, null and even
> for a boolean value without success.  Suggestions?

If no child nodes exist, you could either read the lenght of the
childNodes property of the element in question, or you could compare
the firstChild to null. If it is null, that means the element has no
child.
-- 
David "liorean" Andersson
<uri:http://liorean.web-graphics.com/>



More information about the Javascript mailing list