Connecting Tech Pros Worldwide Help | Site Map

Parsing empty XML nodes in JavaScript

  #1  
Old February 11th, 2006, 12:17 AM
Newbie
 
Join Date: Feb 2006
Posts: 1
Hi there;

I have the following problem. I am using one of the many javascript XML parsing scripts available around the Internet, but the parsing fails when it reach an XML child node that is empty, as in the example below:

<FirstName>John</FirstName>
<MiddleInitial></MiddleInitial>
<LastName>Smith</LastName>

John Smith does not have a middle initial. When the parsing scripts I tried reach this node, it fails with a message such as "Object Required" or some other error message.

I tried the following commands to no avail:

x[i].childNodes[j].firstChild.length == 0
x[i].childNodes[j].firstChild.nodeValue == ""

but it always fail.

What I need is for a way to recognize that that particular field is empty and allow me to put a space or a &nbsp; in its place without crashing.

Any ideas are welcome.

Thanks for your time. :confused:
  #2  
Old June 14th, 2006, 06:28 PM
fen fen is offline
Newbie
 
Join Date: Jun 2006
Posts: 1

re: Parsing empty XML nodes in JavaScript


Anyone know the answer? I am also having the same issue :(
Such a PITA!
Any help would be highly appretiated.
  #3  
Old August 12th, 2006, 07:41 PM
Newbie
 
Join Date: Aug 2006
Posts: 1

re: Parsing empty XML nodes in JavaScript


// Try this guys:

if (xml.getElementsByTagName('job_number')[i].hasChildNodes())
{
rowData += xml.getElementsByTagName('job_number')[i].firstChild.data;
}
else
{
rowData += '';
}
  #4  
Old November 16th, 2006, 10:10 PM
Newbie
 
Join Date: Nov 2006
Posts: 1

re: Parsing empty XML nodes in JavaScript


This works to me....

temp=nodes[i].firstChild;

if (document.getElementById(id)!=null && temp!=null)

Hope u can use it....

Abe.
Reply