Jump to content

Object Undefined/not Found


Guest FirefoxRocks
 Share

Recommended Posts

Guest FirefoxRocks

Something very weird is going on with my JavaScript accessing the XML document.This is the error from Firebug:

tags.getElementsByTagName("level")[0].childNodes[0] is undefined
IE8 says something similar.The weird thing is that this line works fine: tags.getElementsByTagName("name")[0].childNodes[0];The code is like this:
var tags = xmlDoc.getElementsByTagName("tag");			for(i=0; i<tags.length; i++) {		var tagLevel = tags[i].getElementsByTagName("level")[0].childNodes[0].nodeValue;		var name = tags[i].getElementsByTagName("name")[0].childNodes[0].nodeValue;					if(name.search(letters) >= 0) {			// do some stuff with arrays		}	}

The XML file is this:

<?xml version="1.0" encoding="utf-8"?><tags>	<tag>		<level>inline</level>		<name>a</name>	</tag>	<tag>		<level>inline</level>		<name>abbr</name>	</tag></tags>

I've tried several different DOM methods but they all return the same error. What is wrong?

Edited by FirefoxRocks
Link to comment
Share on other sites

That's strange. Have you checked if it has any childNodes?

alert(tags[i].getElementsByTagName("level")[0].childNodes.length);

And if it does

alert(tags[i].getElementsByTagName("level")[0].childNodes.item(0).nodeValue);

Link to comment
Share on other sites

Guest FirefoxRocks
alert(tags[i].getElementsByTagName("level")[0].childNodes.length);

Returns 1 (a whole lot of times)

alert(tags[i].getElementsByTagName("level")[0].childNodes.item(0).nodeValue);

Returns the text in the <level> element a whole lot of times

var tagLevel = tags[i].getElementsByTagName("level")[0].childNodes.item(0).nodeValue;

Returns tags.getElementsByTagName("level")[0].childNodes.item(0) is null

Link to comment
Share on other sites

alert(tags[i].getElementsByTagName("level")[0].childNodes.length);

Returns 1 (a whole lot of times)

alert(tags[i].getElementsByTagName("level")[0].childNodes.item(0).nodeValue);

Returns the text in the <level> element a whole lot of times

var tagLevel = tags[i].getElementsByTagName("level")[0].childNodes.item(0).nodeValue;

Returns tags[i].getElementsByTagName("level")[0].childNodes.item(0) is null

Actually, you shouldn't use var inside a loop. You can only declare a variable once in a Javascript file.Try this:
var tagLevel, name;for(i=0; i<tags.length; i++) {  tagLevel = tags[i].getElementsByTagName("level")[0].childNodes[0].nodeValue;  name = tags[i].getElementsByTagName("name")[0].childNodes[0].nodeValue;  if(name.search(letters) >= 0) {	// do some stuff with arrays  }}

Edited by Ingolme
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...