By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
459,940 Members | 1,328 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 459,940 IT Pros & Developers. It's quick & easy.

using ajax to get document element from xml file

anfetienne
100+
P: 424
Hi Everyone,

I am currently learning AJAX using the book "A Beginners Guide" By Steve Holzner and I am confused as to why a piece of code I've just written to test out what is in the book is not working. My code is below for the html page with the ajax script and also the xml document where the data is, could someone let me know where I have gone wrong? Thanks in advance

***html page
Expand|Select|Wrap|Line Numbers
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
  2. <html xmlns="http://www.w3.org/1999/xhtml">
  3. <head>
  4. <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
  5. <title>Getting the Document Element</title>
  6. <script type="javascript">
  7. function getDocumentElement()
  8. {
  9. var XMLHttpRequestObject = false;
  10.  
  11.     if (window.XMLHttpRequest){
  12.         XMLHttpRequestObject = new XMLHttpRequest();    
  13.     } else if (window.ActiveXObject) {
  14.         XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
  15.     }
  16.  
  17.     if (XMLHttpRequestObject) {
  18.         XMLHttpRequestObject.open("GET", "party.xml", true);
  19.  
  20.         XMLHttpRequestObject.onreadystatechange = function()
  21.         {
  22.             if(XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200){
  23.                 var xmlDocument = XMLHttpRequestObject.responseXML;
  24.                 var documentElement = xmlDocument.documentElement;
  25.                 if(documentElement){
  26.                     document.getElementById("targetDiv").innerHTML = "The document element is &lt;" + documentElement.node + ">.";
  27.                 }
  28.             }
  29.         }
  30.     XMLHttpRequestObject.send(null);
  31.     }
  32. }
  33. </script>
  34. </head>
  35.  
  36. <body>
  37. <h1>Getting the Document Element</h1>
  38.  
  39. <form>
  40. <input type="button" value="Get the document element" onclick="getDocumentElement()" />
  41. </form>
  42. <div id="targetDiv" width =100 height=100>
  43. The result will appear here.
  44. </div>
  45. </body>
  46. </html>
  47.  
  48.  
***xml document
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0">
  2. <!DOCTYPE parties [
  3. <!ELEMENT parties (party*)>
  4. <!ELEMENT party (party_title, party_number, subject, date, people*)>
  5. <!ELEMENT party_title (#PCDATA)>
  6. <!ELEMENT party_number (#PCDATA)>
  7. <!ELEMENT subject (#PCDATA)>
  8. <!ELEMENT date (#PCDATA)>
  9. <!ELEMENT first_name (#PCDATA)>
  10. <!ELEMENT last_name (#PCDATA)>
  11. <!ELEMENT people (person*)>
  12. <!ELEMENT person (first_name,last_name)>
  13. <!ATTLIST party
  14.     type CDATA #IMPLIED>
  15. <!ATTLIST person
  16.     attendance CDATA #IMPLIED>
  17. ]>
  18. <parties>
  19.     <party type="winter">
  20.         <party_title>Snow Day</party_title>
  21.         <party_number>63</party_number>
  22.         <subject>No School Today!</subject>
  23.         <date>22/2/2012</date>
  24.         <people>
  25.             <person attendance="present">
  26.                 <first_name>Andre</first_name>
  27.                 <last_name>Etienne</last_name>
  28.             </person>
  29.             <person attendance="absent">
  30.                 <first_name>Simone</first_name>
  31.                 <last_name>Etienne</last_name>
  32.             </person>
  33.             <person attendance="present">
  34.                 <first_name>Jason</first_name>
  35.                 <last_name>Etienne</last_name>
  36.             </person>
  37.         </people>
  38.     </party>
  39. </parties>
  40.  
Jul 23 '11 #1
Share this Question
Share on Google+
4 Replies


Dormilich
Expert Mod 5K+
P: 8,639
what property is documentElement.node supposed to be? the only useful DOM property I can imagine is tagName (nodeValue would be null, since it is an element node)
Jul 24 '11 #2

anfetienne
100+
P: 424
that is what I was thinking but I'm going by this book... I gave it a try but still it doesn't do anything and the innerHTML doesn't change either
Jul 24 '11 #3

anfetienne
100+
P: 424
It's got me scratching my wig... I even copied and pasted the code that was written in the book
Jul 24 '11 #4

Dormilich
Expert Mod 5K+
P: 8,639
I gave it a try but still it doesn't do anything and the innerHTML doesn't change either
anything in the error console?


I even copied and pasted the code that was written in the book
even books can have errors.
Jul 24 '11 #5

Post your reply

Sign in to post your reply or Sign up for a free account.