Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4
  1. #1
    New Coder
    Join Date
    Jul 2011
    Posts
    19
    Thanks
    6
    Thanked 0 Times in 0 Posts

    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

    Code:
    <!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

    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>

  • #2
    Moderator
    Join Date
    May 2002
    Location
    Hayward, CA
    Posts
    1,461
    Thanks
    1
    Thanked 23 Times in 21 Posts
    xmlDoc would be null if the returned file was not well-formed XML. There's a couple ways this could happen:

    1. The content-type header was not a valid XML content type (it might've been text/plain, for example)
    2. The XML document was not well-formed.

    The latter doesn't appear to be the case (though you do have </CD>>, that's not a well-formedness violation).
    "The first step to confirming there is a bug in someone else's work is confirming there are no bugs in your own."
    June 30, 2001
    author, Verbosio prototype XML Editor
    author, JavaScript Developer's Dictionary
    https://alexvincent.us/blog

  • #3
    New Coder
    Join Date
    Jul 2011
    Posts
    19
    Thanks
    6
    Thanked 0 Times in 0 Posts
    thanks for the reply , i have removed that typo of </CD>> but it is still not working

  • #4
    Supreme Master coder! abduraooft's Avatar
    Join Date
    Mar 2007
    Location
    N/A
    Posts
    14,861
    Thanks
    160
    Thanked 2,223 Times in 2,210 Posts
    Blog Entries
    1
    I get the output "Kenny RogersFor the good times " and I don't get any error in FF.
    The Dream is not what you see in sleep; Dream is the thing which doesn't let you sleep. --(Dr. APJ. Abdul Kalam)


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •