AJAX and Empty Fields
I'm writing an AJAX application that retrieves a dynamically created XML file from a webserver (the xml is created using PHP). I've got the application working great except when the field is empty. In particular, the following XML
<?xml version="1">
<data>
<date>1/2/2005</date>
<time></time>
<event>Something is happening</event>
</data>
As you can see, the time field is empty and when I run the javascript to parse the returned data, I get a javascript error.
I'm using the following javascript to parse the file
function parseXML() {
var theXML = xmlHttpReq.responseXML;
var date= theXML.getElementsByTagName('date')[0].firstChild.data;
var time = theXML.getElementsByTagName('time')[0].firstChild.data;
var event = theXML.getElementsByTagName('event')[0].firstChild.data;
}
The error happens when I try to assign the value to the time variable. The firefox javascript console tells me that firstChild has no properties.
My question: how do I check to see if there is data present? I've tried several different things like
if(typeof(theXML.getElementsByTagName('time')[0].firstChild) != "undefined")
But it's just not working.
Thanks for your help.
|