0

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>

Edited by learner guy: n/a

3
Contributors
3
Replies
6
Views
5 Years
Discussion Span
Last Post by Troy III
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.

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.

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

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.

This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.