K
Kevin
My javascript reads values from an XML file using the code below, then
renders it in HTML.
1 xmlDoc.getElementsByTagName("x"))
[0].childNodes[0].nodeValue
My problem is that the code above fails when x is null, i.e. the XML
file has an empty element.
I wrote a function that walks through each node of the XML tree,
calling the code above on each leaf, i.e. has no children. My code is:
function changeBlankElements(nodeName) // HTML can't render blank
space (""), so we need to replace them
// with
underscores, "_"
{
var length = xmlDoc.getElementsByTagName(nodeName)
[0].childNodes.length; // find out how many children nodeName has
var j=0; // counter variable
var nodeNames=new Array(length); // array of nodeName’s
children.
nodeNames[0] = nodeName;
if(length > 1) // if the nodeName length equals 1, then it has
no children. If it has any children, we need to
// store them in the array nodeNames[]
{
while (j < length)
{
nodeNames[j] = xmlDoc.getElementsByTagName(nodeName)
[0].childNodes[j].nodeName; // load child into array
++j;
}
}
j=0; // need to reset j for next while loop
while (j < length) // here’s where we look at nodeName’s
children. If nodeName’s children has no children, then
// we check if it’s a null element.
Otherwise, i.e. if it has children, we need to run this
// function on those children.
{
if(length==1) // means the nodeName element has no
children, i.e. end branch
{
try // I’m using a try..catch since my program
crashes on the red line since an empty element has
// a null nodeValue, prompting the program to
crash. It crashes since we can’t read the value of
// a null value. I figured a try..catch would fail on the try, then
proceed with the catch // statement. Unfortunately that didn’t
work.
{
if( (xmlDoc.getElementsByTagName(nodeNames[j])
[0].childNodes[0].nodeValue) == null) // if null
{
xmlDoc.getElementsByTagName(nodeNames[j])
[0].childNodes[0].nodeValue = "_"; // replace w/ _
}
}
catch
{
xmlDoc.getElementsByTagName(nodeNames[j])
[0].childNodes[0].nodeValue = "_";
}
}
else // if nodeName, which is the input to this function,
has children, i.e. length > 1, we
// need to call our function with the new input,
nodeNames[j], i.e. the child(ren)
{
changeBlankElements(nodeNames[j]);
}
++j;
}
}
However, this code doesn't work since HTML crashes when I try to read
the nodeValue of an empty XML element.
Please give me an idea as to how I can replace my XML file's empty
elements with underscores so that javascript can read the value, then
HTML can render it.
Thank you,
Kevin
renders it in HTML.
1 xmlDoc.getElementsByTagName("x"))
[0].childNodes[0].nodeValue
My problem is that the code above fails when x is null, i.e. the XML
file has an empty element.
I wrote a function that walks through each node of the XML tree,
calling the code above on each leaf, i.e. has no children. My code is:
function changeBlankElements(nodeName) // HTML can't render blank
space (""), so we need to replace them
// with
underscores, "_"
{
var length = xmlDoc.getElementsByTagName(nodeName)
[0].childNodes.length; // find out how many children nodeName has
var j=0; // counter variable
var nodeNames=new Array(length); // array of nodeName’s
children.
nodeNames[0] = nodeName;
if(length > 1) // if the nodeName length equals 1, then it has
no children. If it has any children, we need to
// store them in the array nodeNames[]
{
while (j < length)
{
nodeNames[j] = xmlDoc.getElementsByTagName(nodeName)
[0].childNodes[j].nodeName; // load child into array
++j;
}
}
j=0; // need to reset j for next while loop
while (j < length) // here’s where we look at nodeName’s
children. If nodeName’s children has no children, then
// we check if it’s a null element.
Otherwise, i.e. if it has children, we need to run this
// function on those children.
{
if(length==1) // means the nodeName element has no
children, i.e. end branch
{
try // I’m using a try..catch since my program
crashes on the red line since an empty element has
// a null nodeValue, prompting the program to
crash. It crashes since we can’t read the value of
// a null value. I figured a try..catch would fail on the try, then
proceed with the catch // statement. Unfortunately that didn’t
work.
{
if( (xmlDoc.getElementsByTagName(nodeNames[j])
[0].childNodes[0].nodeValue) == null) // if null
{
xmlDoc.getElementsByTagName(nodeNames[j])
[0].childNodes[0].nodeValue = "_"; // replace w/ _
}
}
catch
{
xmlDoc.getElementsByTagName(nodeNames[j])
[0].childNodes[0].nodeValue = "_";
}
}
else // if nodeName, which is the input to this function,
has children, i.e. length > 1, we
// need to call our function with the new input,
nodeNames[j], i.e. the child(ren)
{
changeBlankElements(nodeNames[j]);
}
++j;
}
}
However, this code doesn't work since HTML crashes when I try to read
the nodeValue of an empty XML element.
Please give me an idea as to how I can replace my XML file's empty
elements with underscores so that javascript can read the value, then
HTML can render it.
Thank you,
Kevin