Click to See Complete Forum and Search --> : Test for an empty XML tag


plattecoducks
03-23-2009, 04:33 PM
My client conducts events (seminars) in his store after hours. He wants me to keep the schedule up to date. I suggested I knew an easy technology that would work. I want to use an XML file that he can upload to the server when he wants to update the schedule. The file is of the following format:

<events>
<event>
<date>Some date here</date>
<time>Some time here</time>
<remark>Something about the event</remark>
</event>
<!-- etc., etc., etc., -->
</events>

I tested it pretty thoroughly, gave him a lesson on structure of the file and went on about my business. Now he reports to me that the table I set up for the events does not display. I examined the XML file and the first thing I noticed was that the <remark></remark> tag was empty.

I thought about it a while and realized that this could happen in his line of business; that this indeed could be empty, meaning it is open to anyone and no special invitation is required. That the idea of the table.

So I sat down to modify the code and discovered I don't know how to write a suitable test for an empty XML tag. If the tag is empty the javascript code falls down and just exits the routine.

Has anyone ever seen this? What am I missing? What is the value of an empty object pulled from an XML file? Is it Null, undefined or what? Help! This should be a piece of cake for somebody.

JohnBampton
03-23-2009, 07:01 PM
no one knows what you are doing unless you post your code

plattecoducks
03-23-2009, 08:02 PM
The routine works just fine when all tags have values. The problem occurs when a tag has no value; in this case the <remark></remark> tag. There need not always be a remark. Below is a snippet of the XLM file for an example:

<events>
<table>
<row>
<event>Anxiety in Kids</event>
<date>3 April, 2009</date>
<remark></remark>
</row>
<!-- etc., etc. -->
</table>
</events>


Here is the routine, a function with one parameter being the XML file contents:


function getXMLSuccessEvent(originalRequest) {
var response = originalRequest.responseXML;
// Start formatting table:
var retTable = "<table cols='3'>\n";
// capture all events:
var events = response.getElementsByTagName('row');
// capture individual events:
var evt = response.getElementsByTagName('event');
// capture dates:
var dte = response.getElementsByTagName('date');
// capture remarks:
var rmk = response.getElementsByTagName('remark');
// loop through events:
for ( var i = 0; i < events.length; i++ ) {
// table row:
retTable += "<tr>\n";
// event name cell:
retTable += "<td>\n";
retTable += evt[i].firstChild.data;
retTable += "</td>\n";
// event date cell:
retTable += "<td>\n";
retTable += dte[i].firstChild.data;
retTable += "</td>\n";
/**
Here is where the test for an empty tag needs to go:
**/
// remarks cell:
retTable += "<td>\n";
retTable += rmk[i].firstChild.data;
retTable += "</td>\n";
// close row:
retTable += "</tr>\n";
}
// close the table:
retTable += "</table>\n";
// Code here to return the formatted table to the page
}


If all the tags in the XML file have values this works perfectly. The problem is when there is an empty cell. As stated previously, there can be an empty <remark></remark> tag. When there is, then the code just exits and returns nothing.

So I need to write a test for this empty tag and do something like supply a space or some text character.

plattecoducks
03-24-2009, 11:04 AM
OK, this has become an academic exercise. I've redesigned how this works for my client.

There is still a hanging question: Why does this happen? My guess is that when there is an empty tag, the script I wrote can't handle the return, and/or perhaps there is no return: I don't know which. But the script just terminates with no indication that there was an error. What is going on?

jkmyoung
03-24-2009, 12:24 PM
var rmk = response.getElementsByTagName('remark');
retTable += rmk[i].firstChild.data;

You get ane error here, because the rmk node has no children.
rmk[i] = returns first i'th node.
rmk[i].firstChild = null
rmk[i].firstChild.data => Calling function on null object error.


Check for null:
if (rmk[i].firstChild != null)
retTable += rmk[i].firstChild.data;

plattecoducks
03-24-2009, 01:34 PM
Well, that must have been a case of standing in the forest and not seeing the trees. That would be the solution.

Thanks, jkmyoung.