Dealing with empty XML nodes (Prototype)

I have the following XML data:


<data>
<wp_hours_on>24.00</wp_hours_on>
<wp_tubing_press>30</wp_tubing_press>
<wp_casing_press>0</wp_casing_press>
<wp_volume>43</wp_volume>
<wp_current_rate>38</wp_current_rate>
<wp_comments/>
<sc_volume>42</sc_volume>
<sc_current_rate>44</sc_current_rate>
<fm_volume>0</fm_volume>
<c_hours_on>24.00</c_hours_on>
<c_suction>19</c_suction>
<c_discharge>0</c_discharge>
<c_comments>SIBU @ 3:00 p.m</c_comments>
<ot589_top_ft>8</ot589_top_ft>
<ot589_top_in>8</ot589_top_in>
<ot589_bt_ft>0</ot589_bt_ft>
<ot589_bt_in>7</ot589_bt_in>
<ot589_prod>0.00</ot589_prod>
</data>

Currently, this data is refreshed using an AJAX call from Prototype. The issue that comes up is that sometimes, different nodes will have empty data. I need to check each node on every refresh to clear out a form field value if that node is empty.

Any ideas on how to do this? I’ve tried checking for an empty string, but that doesn’t work. Now I’m thinking that I should just check to see if the node exists but I have my doubts that that will work either.

What do you find that the empty node has instead?

Paul, essentially the node is empty. I need a way to go through the nodes and ignore the empty ones. As soon as the Javascript hits the empty node it stops running.

So when I run this:


$('wp_comments').value = transport.responseXML.getElementsByTagName('wp_comments')[0].firstChild.nodeValue;

and the <wp_comments> element is empty (<wp_comments />) I need to skip that element and move on to the next one?

I tried this as well:


cNode = transport.responseXML.getElementsByTagName('wp_comments')[0];
if(cNode.ChildNodes.length) {
$('wp_comments').value = transport.responseXML.getElementsByTagName('wp_comments')[0].firstChild.nodeValue;
}

and it does not seem to work.
Thanks for the help!

So would it work appropriately if you checked if there are any comments?


var comments = transport.responseXML.getElementsByTagName('wp_comments');
if (comments) {
    $('wp_comments').value = comments[0].firstChild.nodeValue;
}

Check its length instead.

I did:


var comments = transport.responseXML.getElementsByTagName('wp_comments');
if(comments) {
     alert("true!");
}

And even if the node is empty, the alert pops up.

Great idea! Tried this:


var comments = transport.responseXML.getElementsByTagName('wp_comments')[0].firstChild.nodeValue;
		if(comments.length > 0)
		{
			alert(comments.length);
			$('wp_comments').value = transport.responseXML.getElementsByTagName('wp_comments')[0].firstChild.nodeValue;
		}

It correctly returns the length if there is one.

Now, I need to apply this to every node I have. Any ideas on consolidating the “if” statements?

Thanks!

Playing with this some more, it appears that the empty node still breaks the XML structure, even if I ignore it in the JavaScript.

I may just need to modify the script that generates the XML to not include blank items. So if my value is null in the database, just don’t generate a tag for that item.