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 8 of 8
  1. #1
    New Coder
    Join Date
    Jul 2012
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts

    Javascript XML Help!

    I am writing some simple JavaScript code that looks at an XML file on the same server for data to fill a drop down list with. Every time I run the code (even though I did it the way everyone else says to), I get Type Error: cannot call method 'getElementsByTagName' of null.

    Here is my JavaScript code:
    Code:
    function ReadErrorList()
    {
    	var xmlhttp;
      if (window.XMLHttpRequest)
        {// code for IE7+, Firefox, Chrome, Opera, Safari
        var xmlhttp=new XMLHttpRequest();
        }
      else
        {// code for IE6, IE5
        var xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
        }
        
      try
      	{
      	xmlhttp.open("GET","errorlist.xml",false);
      	xmlhttp.send();
      	var xmlDoc=xmlhttp.responseXML;
    		}
    	catch (e)
    		{
    			alert("An error occured while loading the configuration file." + e);
    		}
    	
    	try
    	{	
      	var x=xmlDoc.getElementsByTagName("errortype");
    	}
     catch (e)
     	{
     		alert("An error occured in xml. " + e);
    	}
      for (i=0;i<x.length;i++)
        { 
        	var opt=document.createElement("option");
        	document.getElementById("IssueType").options.add(opt);
    			
    			var y=x[i].getElementsByTagName("text")[0].childNodes[0].nodeValue;
    			opt.text = y;
    			opt.value = y;
    		}
    }
    And here is my XML file:
    Code:
    <?xml version="1.0" encoding="ISO-8859-1"?>
    
    <errorlist>
    		<errortype>
    			<text>Engineering Issue</text>
    			<recipiant>0, 1</recipiant>
    		</errortype>
    		<errortype>
    			<text>Commercial Issue</text>
    			<recipiant>0</recipiant>
    		</errortype>
    		<errortype>
    			<text>Traffic Pulse Issue</text>
    			<recipiant>0</recipiant>
    		</errortype>
    		<errortype>
    			<text>Song Issue</text>
    			<recipiant>0</recipiant>
    		</errortype>
    		<errortype>
    			<text>News Issue</text>
    			<recipiant>0</recipiant>
    		</errortype>
    		<errortype>
    			<text>Issue Of Unknown Origin</text>
    			<recipiant>0</recipiant>
    		</errortype>
    		<errortype>
    			<text>Other Issue</text>
    			<recipiant>0, 1</recipiant>
    		</errortype>
    </errorlist>
    Please help if you can. Thanks

  • #2
    Regular Coder
    Join Date
    Aug 2010
    Posts
    967
    Thanks
    19
    Thanked 211 Times in 209 Posts
    Your code works
    when hosted on my server.

    http://codingforums.daveyerwin.com/patrickfrog/
    Last edited by DaveyErwin; 09-01-2013 at 05:33 PM.

  • Users who have thanked DaveyErwin for this post:

    patrickfrog (09-05-2013)

  • #3
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,168
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    I don't have an older version of MSIE (using IE 10 now) sitting around, but I vaguely recall that var xmlDoc=xmlhttp.responseXML; did not work in IE7 and maybe not in IE8.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #4
    New Coder
    Join Date
    Jul 2012
    Posts
    10
    Thanks
    2
    Thanked 0 Times in 0 Posts
    I am attempting to build and test this code in a Linux, Raspberry Pie. Do you think that this could be the issue?

  • #5
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,168
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    The browser will be the issue. Dunno what browser Raspberry Pi uses.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #6
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,296
    Thanks
    10
    Thanked 583 Times in 564 Posts
    make sure the xml file is being served as "text/xml" or something with xml in it. If you use text/plain or text/html, you don't get an XML dom object from ajax, you'll get null.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%

  • #7
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,168
    Thanks
    75
    Thanked 4,338 Times in 4,304 Posts
    Good catch, RndMe. Though again this depends on the browser. Chrome seems especially tolerant of errors like that, though I haven't tested this specific one.
    An optimist sees the glass as half full.
    A pessimist sees the glass as half empty.
    A realist drinks it no matter how much there is.

  • #8
    Senior Coder rnd me's Avatar
    Join Date
    Jun 2007
    Location
    Urbana
    Posts
    4,296
    Thanks
    10
    Thanked 583 Times in 564 Posts
    Quote Originally Posted by Old Pedant View Post
    Good catch, RndMe. Though again this depends on the browser. Chrome seems especially tolerant of errors like that, though I haven't tested this specific one.
    my guess is that this is being served from a folder instead of from a webserver. if the browser is chrome, one might be able to modify the local behavior using command-line switches. if the ajax works on responseText, one can parse a string into a new XML document, and continue on from there as expected.
    my site (updated 13/9/26)
    BROWSER STATS [% share] (2014/5/28) IE7:0.1, IE8:5.3, IE11:8.4, IE9:3.2, IE10:3.2, FF:18.2, CH:46, SF:7.9, NON-MOUSE:32%


  •  

    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
    •