Connecting Tech Pros Worldwide Forums | Help | Site Map

Parsing empty XML nodes in JavaScript

Newbie
 
Join Date: Feb 2006
Posts: 1
#1: Feb 11 '06
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:

fen fen is offline
Newbie
 
Join Date: Jun 2006
Posts: 1
#2: Jun 14 '06

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.
Newbie
 
Join Date: Aug 2006
Posts: 1
#3: Aug 12 '06

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 += '';
}
Newbie
 
Join Date: Nov 2006
Posts: 1
#4: Nov 16 '06

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


Similar JavaScript / Ajax / DHTML bytes