using ajax to get document element from xml file

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • anfetienne
    Contributor
    • Feb 2009
    • 424

    using ajax to get document element from xml file

    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
    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">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>Getting the Document Element</title>
    <script type="javascript">
    function getDocumentElement()
    {
    var XMLHttpRequestObject = false;
    	
    	if (window.XMLHttpRequest){
    		XMLHttpRequestObject = new XMLHttpRequest();	
    	} else if (window.ActiveXObject) {
    		XMLHttpRequestObject = new ActiveXObject("Microsoft.XMLHTTP");
    	}
    	
    	if (XMLHttpRequestObject) {
    		XMLHttpRequestObject.open("GET", "party.xml", true);
    		
    		XMLHttpRequestObject.onreadystatechange = function()
    		{
    			if(XMLHttpRequestObject.readyState == 4 && XMLHttpRequestObject.status == 200){
    				var xmlDocument = XMLHttpRequestObject.responseXML;
    				var documentElement = xmlDocument.documentElement;
    				if(documentElement){
    					document.getElementById("targetDiv").innerHTML = "The document element is &lt;" + documentElement.node + ">.";
    				}
    			}
    		}
    	XMLHttpRequestObject.send(null);
    	}
    }
    </script>
    </head>
    
    <body>
    <h1>Getting the Document Element</h1>
    
    <form>
    <input type="button" value="Get the document element" onclick="getDocumentElement()" />
    </form>
    <div id="targetDiv" width =100 height=100>
    The result will appear here.
    </div>
    </body>
    </html>
    ***xml document
    Code:
    <?xml version="1.0">
    <!DOCTYPE parties [
    <!ELEMENT parties (party*)>
    <!ELEMENT party (party_title, party_number, subject, date, people*)>
    <!ELEMENT party_title (#PCDATA)>
    <!ELEMENT party_number (#PCDATA)>
    <!ELEMENT subject (#PCDATA)>
    <!ELEMENT date (#PCDATA)>
    <!ELEMENT first_name (#PCDATA)>
    <!ELEMENT last_name (#PCDATA)>
    <!ELEMENT people (person*)>
    <!ELEMENT person (first_name,last_name)>
    <!ATTLIST party
    	type CDATA #IMPLIED>
    <!ATTLIST person
    	attendance CDATA #IMPLIED>
    ]>
    <parties>
    	<party type="winter">
    		<party_title>Snow Day</party_title>
    		<party_number>63</party_number>
    		<subject>No School Today!</subject>
    		<date>22/2/2012</date>
    		<people>
    			<person attendance="present">
    				<first_name>Andre</first_name>
    				<last_name>Etienne</last_name>
    			</person>
    			<person attendance="absent">
    				<first_name>Simone</first_name>
    				<last_name>Etienne</last_name>
    			</person>
    			<person attendance="present">
    				<first_name>Jason</first_name>
    				<last_name>Etienne</last_name>
    			</person>
    		</people>
    	</party>
    </parties>
  • Dormilich
    Recognized Expert Expert
    • Aug 2008
    • 8694

    #2
    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)

    Comment

    • anfetienne
      Contributor
      • Feb 2009
      • 424

      #3
      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

      Comment

      • anfetienne
        Contributor
        • Feb 2009
        • 424

        #4
        It's got me scratching my wig... I even copied and pasted the code that was written in the book

        Comment

        • Dormilich
          Recognized Expert Expert
          • Aug 2008
          • 8694

          #5
          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.

          Comment

          Working...