www.webdeveloper.com
Results 1 to 2 of 2

Thread: XML HTTP Request Object (help!)

  1. #1
    Join Date
    Apr 2011
    Posts
    9

    XML HTTP Request Object (help!)

    Hi guys,

    I am using this code to try and pull some XML data from an external file into the HTML file. It uses the XML HTTP Request object and as far as I can see, it should work. The html appears on the page, but the information from the XML file does not:

    <body>

    <script type="text/javascript">
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
    http=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
    http=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.open("GET","note.xml",false);
    xmlhttp.send();
    xmlDoc=xmlhttp.responseXML;

    document.getElementById("to").innerHTML=
    xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue;
    document.getElementById("from").innerHTML=
    xmlDoc.getElementsByTagName("from")[0].childNodes[0].nodeValue;
    document.getElementById("message").innerHTML=
    xmlDoc.getElementsByTagName("body")[0].childNodes[0].nodeValue;

    </script>


    <h1>W3Schools Internal Note</h1>
    <div>
    <b>To:</b> <span id="to"></span><br />
    <b>From:</b> <span id="from"></span><br />
    <b>Message:</b> <span id="message"></span>
    </div>
    </body>
    </html>


    The external XML file contains:

    <?xml version="1.0" encoding="ISO-8859-1" ?>
    <note>
    <to>Tove</to>
    <from>Jani</from>
    <heading>Reminder</heading>
    <body>Don't forget me this weekend!</body>
    </note>


    I really cannot see why this isnt working. Anyone have any ideas? Any help would be massively appreciated 'cus this is driving me insane.

    Thanks very much.

  2. #2
    Join Date
    Jan 2010
    Location
    Germany
    Posts
    10
    upps

    Code:
    <html>
    <body>
    
    <script type="text/javascript">
    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
    xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
    xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }
    xmlhttp.open("GET","note.xml",false);
    xmlhttp.send();
    xmlDoc=xmlhttp.responseXML;
    
    document.getElementById("to").innerHTML=
    xmlDoc.getElementsByTagName("to")[0].childNodes[0].nodeValue;
    document.getElementById("from").innerHTML=
    xmlDoc.getElementsByTagName("from")[0].childNodes[0].nodeValue;
    document.getElementById("message").innerHTML=
    xmlDoc.getElementsByTagName("body")[0].childNodes[0].nodeValue;
    
    </script>
    
    
    <h1>W3Schools Internal Note</h1>
    <div>
    <b>To:</b> <span id="to"></span><br />
    <b>From:</b> <span id="from"></span><br />
    <b>Message:</b> <span id="message"></span>
    </div>
    </body>
    </html>


    so it will work on ie8 firefox4 opera11

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

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



Recent Articles