954,561 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Uncaught TypeError: Cannot call method 'getElementsByTagName' of null

Hi i am learning xml , in a very simple code i got an error saying
"Uncaught TypeError: Cannot call method 'getElementsByTagName' of null"
i am getting this in all browsers ,i have tried to look for solution on google but could not find ,Plz help

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml"><title>http request</title>
<head>
<link rel="shortcut icon" href="twitterfinalcut/twittericon.jpg" />

</head>
<body>

<script type="text/javascript">
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
 var  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
xmlhttp.open("GET","note.xml",false);
xmlhttp.send();
xmlDoc=xmlhttp.responseXML; 
  var x=xmlDoc.getElementsByTagName("CD");
  document.write (x[0].getElementsByTagName("ARTIST")[0].childNodes[0].nodeValue);
  document.write (x[0].getElementsByTagName("TITLE")[0].childNodes[0].nodeValue);
 </script>

</body>
</html>


Here is the simple xml file "note.xml" code

<?xml version="1.0" encoding="ISO-8859-1"?>


<CATALOG>
   <CD>
        <ARTIST>Kenny Rogers</ARTIST>
		<TITLE>For the good times</TITLE>

		

		<COUNTRY>UK</COUNTRY>

		<COMPANY>Mucik Master</COMPANY>

		<PRICE>8.70</PRICE>

		<YEAR>1995</YEAR>
  </CD>>
</CATALOG>
learner guy
Light Poster
26 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

If the XML file you are retrieving has invalid markup, then on line 20 xmlDoc will be set to null. If that is the case, then xmlDoc.getElementsByTagName() will give you the error you are describing. So, you need to make sure that the responseXML is not null.

On your XML file, line 18 has a typo, making your xml markup invalid.

hielo
Veteran Poster
1,124 posts since Dec 2007
Reputation Points: 116
Solved Threads: 244
 

If the XML file you are retrieving has invalid markup, then on line 20 xmlDoc will be set to null. If that is the case, then xmlDoc.getElementsByTagName() will give you the error you are describing. So, you need to make sure that the responseXML is not null.

On your XML file, line 18 has a typo, making your xml markup invalid.


thanks for the reply , i have removed that typo of> but it is still not working :(

learner guy
Light Poster
26 posts since Aug 2011
Reputation Points: 10
Solved Threads: 0
 

Try
var x=xmlDoc.getElementsByTagName("CD"); alert(x)

If your alert doesn't contain what you asked for, it means that your xhtml request failed. So you should examine your xmlhttp request code in more detail and see if its complete.

Troy III
Practically a Master Poster
609 posts since Jun 2008
Reputation Points: 120
Solved Threads: 80
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You
View similar articles that have also been tagged: