getElementByTagName("x") doesn't render in HTML

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
 
T

Thomas 'PointedEars' Lahn

Kevin wrote:
^^^^^
Kevin who?
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

What is this?
My problem is that the code above fails when x is null, i.e. the XML
file has an empty element.

There is no `x' there that could become `null'.
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.

Looks like a waste of runtime.
My code is:

function changeBlankElements(nodeName) // HTML can't render blank
space (""), so we need to replace them
// with
underscores, "_"

Utter nonsense.
{
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.

Looks like a waste of memory. However, using XPath instead might have
helped.
[snip unreadable code]

Learn to document your code properly, then we can perhaps talk again.
However, this code doesn't work since HTML crashes

"HTML crashes"? Are you sure you know what you are doing?
when I try to read the nodeValue of an empty XML element.

That is unsurprising. Don't do that, then.
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.

The reasonable approach would be to *import* the XML subtree containing the
HTML markup that should be rendered, and then *append* the result as *child*
of an HTML element.


PointedEars
 
T

transkawa

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

use a filter. if(null){dothis} else {dothat}.
you should follow the advise of pointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,995
Messages
2,570,228
Members
46,818
Latest member
SapanaCarpetStudio

Latest Threads

Top