Blank XML tags are killing me. Please help if it's easy for you!

P

Peach

Hi there-

I am new to javascript and I am simply trying to display XML data in
an html doc using javascript. When I try to display a set of elements
and one or more of them are empty XML tags (ie. <show1></show1> OR </
show1>) it crashes my page. I don't know how to fix it.

Here is some code, which I think will give you an idea of what I am
trying to do:

var x=xmlDoc.getElementsByTagName("innovative");

function show(i)
{

name=(x.getElementsByTagName("name")[0].childNodes[0].nodeValue);
nomination=(x.getElementsByTagName("nomination")
[0].childNodes[0].nodeValue);
show1=(x.getElementsByTagName("show1")[0].childNodes[0].nodeValue);
show2=(x.getElementsByTagName("show2")[0].childNodes[0].nodeValue);
link1=(x.getElementsByTagName("link1")[0].childNodes[0].nodeValue);
link2=(x.getElementsByTagName("link2")[0].childNodes[0].nodeValue);

txt="Comic: "+name+show1test+ said:
Upcoming Sets: <br/>"+show1+"<br />"+show2+"<br />Links: <a
target=_blank href="+link1+">"+link1+"</a><br />"+"<a target=_blank
href="+link2+">"+link2+"</a>" ;

document.getElementById("show").innerHTML=txt;
}

Can anyone help? I really want this page to work.
 
W

Walton

Hi there-

I am new to javascript and I am simply trying to display XML data in
an html doc using javascript.  When I try to display a set of elements
and one or more of them are empty XML tags (ie. <show1></show1> OR </
show1>) it crashes my page.  I don't know how to fix it.

Here is some code, which I think will give you an idea of what I am
trying to do:

var x=xmlDoc.getElementsByTagName("innovative");

function show(i)
{

name=(x.getElementsByTagName("name")[0].childNodes[0].nodeValue);
nomination=(x.getElementsByTagName("nomination")
[0].childNodes[0].nodeValue);
show1=(x.getElementsByTagName("show1")[0].childNodes[0].nodeValue);
show2=(x.getElementsByTagName("show2")[0].childNodes[0].nodeValue);
link1=(x.getElementsByTagName("link1")[0].childNodes[0].nodeValue);
link2=(x.getElementsByTagName("link2")[0].childNodes[0].nodeValue);


these types of calls will fail on empty tags because an empty tag
doesn't have a childNodes[0]. by
indexing .getElementsByTagName("show1")[0] and .childNodes[0] you're
assuming that these objects always exist, which they might not. to
avoid the problem you should test to see if those objects exist before
you index them.

Can anyone help?  I really want this page to work.

here's a little function I use to do exactly what you are doing:


function getNodeValue(node, tagname, default_val) {
var elems = node.getElementsByTagName(tagname);

//test to see if tags aren't blank and have some value
if (elems && elems[0] && elems[0].firstChild &&
elems[0].firstChild.nodeValue) {
return elems[0].firstChild.nodeValue;
}
return default_val;
}



to make this call, for example:
show1=(x.getElementsByTagName("show1")[0].childNodes[0].nodeValue);

you would do this:
getNodeValue(x,'show1')

or if you want show1 to have some default value like "no show" when it
is blank, call this:
getNodeValue(x,'show1','no show');

hope that helps,
John
 

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

Forum statistics

Threads
473,774
Messages
2,569,598
Members
45,152
Latest member
LorettaGur
Top