Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Checking for Empty Elements in Javascript--Help!!

Status
Not open for further replies.

AlexTekTip

Programmer
Aug 30, 2004
17
US
How do I check if an element of an XML document is empty?

I have the following code in Javascript:

var vSubA = vAgsXml.document.getElementsByTagName("_f8");
vSubA = vSubA[0].firstChild.data (This line errors out when element _f8 is empty; I guess because you can't assign something that does not exist).

Is there a way to check if the node is valid so I know whether to assign or not?

The below shows the XML document when the _f8 element is empty:

<?xml version="1.0" encoding="ISO-8859-1" ?>
- <XZQGL.1>
- <ZQGL.1>
<_PDL>DEV</_PDL>
<_TKN>ZQGL.1</_TKN>
<_OUT>XML</_OUT>
<_TDS>Ignore</_TDS>
<_EVT>CHG</_EVT>
<_f0>ZQGL.1</_f0>
<_f1>I</_f1>
<_f2>3800</_f2>
<_f3>STC022</_f3>
<_f4>00500</_f4>
<_f5>B</_f5>
<_f6>2174319</_f6>
<_f7>512215</_f7>
<_f8 />
<FldNbr>_f0</FldNbr>
<MsgNbr>000</MsgNbr>
<Message>Inquiry Complete</Message>
</ZQGL.1>
</XZQGL.1>


Thank you!!!
 
Would this help?

var mylist = xmlDoc.getElementsByTagName("nodename");
for (var x = 0;x<= mylist.length-1; x++)
{
if((mylist[x].text) != ''){
alert (mylist[x].text);
// or do whatever
}
}
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top