SitePoint Sponsor

User Tag List

Results 1 to 9 of 9

Hybrid View

  1. #1
    SitePoint Enthusiast
    Join Date
    Dec 2006
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    new to xml - simple responseXML example won't work

    Hi all,

    I've recently been assigned an xml task so I started trying sample code from a popular tutorial site. I can't get any of the samples that use "responseXML" to work. I've been struggling with it for about 3 days and still no luck.

    I think it must be something fundamental that I'm leaving out

    Would someone please show me what's wrong. I always get the error: "object required" when I try to use:

    Code:
    xmlDoc.getElementsByTagName("CATALOG")[0].childNodes[0].nodeValue;
    Here's the html:
    HTML Code:
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" 
    "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1" />
    <title>Untitled Document</title>
    
    <script type="text/javascript">
    function loadxml(){
    	if (window.XMLHttpRequest){
    		xhttp=new XMLHttpRequest()
    	} else {
    		xhttp=new ActiveXObject("Microsoft.XMLHTTP")
    	}
    	xhttp.open("GET","cd_catalog.xml",false);
    	xhttp.send("");
    	xmlDoc=xhttp.responseXML;
    }
    
    function showcatalog(){
    	document.getElementById("display_catalog").innerHTML=
    	xmlDoc.getElementsByTagName("CATALOG")[0].childNodes[0].nodeValue;
    }
    function showcd(){
    	document.getElementById("display_cd").innerHTML=
    	xmlDoc.getElementsByTagName("CD")[0].childNodes[0].nodeValue;
    }
    function showtitle(){
    	document.getElementById("display_title").innerHTML=
    	xmlDoc.getElementsByTagName("TITLE")[0].childNodes[0].nodeValue;
    }
    </script>
    </head>
    
    <body>
    	<h1>Click a button</h1>
    	<input id="load" type"button" value="load xml" onclick="loadxml()"></input>
    	
    	<input id="btn1" type"button" value="button1"  onclick="showcatalog()"></input>
    	<input id="btn1" type"button" value="button2" onclick="showcd()"></input>
    	<input id="btn1" type"button" value="button3" onclick="showtitle()"></input>
    
    	<p id="display_catalog"></p>
    	<p id="display_cd"></p>
    	<p id="display_title"></p>
    </body>
    </html> 
    Here's the xml doc:
    Code:
    <?xml version="1.0" encoding="ISO-8859-1" ?> 
    - <CATALOG>
    - <CD>
      <TITLE>Empire Burlesque</TITLE> 
      <ARTIST>Bob Dylan</ARTIST> 
      <COUNTRY>USA</COUNTRY> 
      <COMPANY>Columbia</COMPANY> 
      <PRICE>10.90</PRICE> 
      <YEAR>1985</YEAR> 
      </CD>
    - <CD>
      <TITLE>Hide your heart</TITLE> 
      <ARTIST>Bonnie Tyler</ARTIST> 
      <COUNTRY>UK</COUNTRY> 
      <COMPANY>CBS Records</COMPANY> 
      <PRICE>9.90</PRICE> 
      <YEAR>1988</YEAR> 
      </CD>
    - <CD>
      <TITLE>Greatest Hits</TITLE> 
      <ARTIST>Dolly Parton</ARTIST> 
      <COUNTRY>USA</COUNTRY> 
      <COMPANY>RCA</COMPANY> 
      <PRICE>9.90</PRICE> 
      <YEAR>1982</YEAR> 
      </CD>
    - <CD>
      <TITLE>Still got the blues</TITLE> 
      <ARTIST>Gary Moore</ARTIST> 
      <COUNTRY>UK</COUNTRY> 
      <COMPANY>Virgin records</COMPANY> 
      <PRICE>10.20</PRICE> 
      <YEAR>1990</YEAR> 
      </CD>
    - <CD>
      <TITLE>Eros</TITLE> 
      <ARTIST>Eros Ramazzotti</ARTIST> 
      <COUNTRY>EU</COUNTRY> 
      <COMPANY>BMG</COMPANY> 
      <PRICE>9.90</PRICE> 
      <YEAR>1997</YEAR> 
      </CD>
    </CATALOG>

  2. #2
    SitePoint Evangelist TommiChi's Avatar
    Join Date
    Oct 2008
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Ok, first, just to make sure you are really serving xml, you need to set the request header to xml. aslo, you can't read the xml until the readystate is "ready". will send code sample in the morning when i'm in front of my computer

  3. #3
    SitePoint Evangelist TommiChi's Avatar
    Join Date
    Oct 2008
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    OK, so here's a dummy function for you:
    function myAJAXget() {

    var ajaxGet;
    try {
    ajaxGet = new XMLHttpRequest();
    ajaxGet.open("GET", filename, true);
    ajaxGet.setRequestHeader("Content-Type", "text/xml");
    }
    catch (err) {
    ajaxGet = new ActiveXObject("Microsoft.XMLHTTP");
    ajaxGet.open("GET", filename, true);
    ajaxGet.setRequestHeader("Content-Type", "text/xml");
    }
    ajaxGet.onreadystatechange = function() {

    if (ajaxGet.readyState == 4) {
    var theResponse = ajaxGet.responseXML;
    var theRoot = theResponse.documentElement; //Get the root element
    var theText = theRoot.getElementsByTagName(TargetNodeName)[x].childNodes[0].nodeValue; //You can replace x with any number you need
    targetElement.innerHTML = theText;
    }

    }
    ajaxGet.send(null);

    }

  4. #4
    SitePoint Enthusiast
    Join Date
    Dec 2006
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Hey thanks so much TommiChi.

    I really was missing something fundamental. I guess the tutorial examples that I've seen could use some serious fleshing out. I'll try running your code when I get home tonight.

  5. #5
    SitePoint Enthusiast
    Join Date
    Dec 2006
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    It works!! Thanks again TommiChi.

  6. #6
    SitePoint Evangelist TommiChi's Avatar
    Join Date
    Oct 2008
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    no problem. glad it helped

  7. #7
    SitePoint Enthusiast
    Join Date
    Dec 2006
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Well OK so it mostly works, in IE that is. FF is throwing an error, and it seems to be a fairly common error. I just haven't been able to understand the solutions that I've seen.

    I'm getting this in FF:
    Code:
    theRoot.getElementsByTagName("reviewyear")[i] is undefined
    The other posts I've read seem to indicate that this problem occurs if you don't use XMLHttpRequest(), but I am, so I'm lost on this.

    Can anyone help?

    Here's the js I'm using:
    Code:
    function showReviews() {
      var ajaxGet;
      try {
        ajaxGet = new XMLHttpRequest();
        ajaxGet.open("GET", "reviewtest.xml", true);
        ajaxGet.setRequestHeader("Content-Type", "text/xml");
      }
      catch (err) {
        ajaxGet = new ActiveXObject("Microsoft.XMLHTTP");
        ajaxGet.open("GET", "reviewtest.xml", true);
        ajaxGet.setRequestHeader("Content-Type", "text/xml");
      }
    	
      ajaxGet.onreadystatechange = function() {
        if (ajaxGet.readyState == 4) {
          var theResponse = ajaxGet.responseXML;
          var theRoot = theResponse.documentElement; // get the root element
          var tempYears = new Array;
    
          for(i = 0; i < theRoot.childNodes.length; i++){
            //// the problem is here: "theRoot.getElementsByTagName("reviewyear")[i] is undefined"
            tempYears[i] = theRoot.getElementsByTagName("reviewyear")[i].childNodes[0].nodeValue;
          }
        }
      }
      ajaxGet.send(null);
    }
    and here's the xml:
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <archive>
    <review>
    <review_id>1</review_id>
    <filmtitle>Test</filmtitle>
    <director>Director 1</director>
    <producer>Producer 1</producer>
    <filmyear>2001</filmyear>
    <reviewtitle>Review Title 1</reviewtitle>
    <caption>Review Caption 1</caption>
    <reviewday>15</reviewday>
    <reviewmonth>1</reviewmonth>
    <reviewyear>2009</reviewyear>
    </review>
    <review>
    <review_id>2</review_id>
    <filmtitle>Test 2</filmtitle>
    <director>Director 2</director>
    <producer>Producer 2</producer>
    <filmyear>2002</filmyear>
    <reviewtitle>Review Title 2</reviewtitle>
    <caption>Review Caption 2</caption>
    <reviewday>16</reviewday>
    <reviewmonth>2</reviewmonth>
    <reviewyear>2009</reviewyear>
    </review>
    </archive>

  8. #8
    SitePoint Evangelist TommiChi's Avatar
    Join Date
    Oct 2008
    Posts
    440
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Does it return the error you mention for every value you set for "i"? What happens whn you set "i" to zero (return the first node that matches the specified name?

  9. #9
    SitePoint Enthusiast
    Join Date
    Dec 2006
    Posts
    45
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    When I use '0' instead of 'i' I don't get the error. However, I get a similar error on a subsequent line that uses getElementsByTagName even if I use '0' as the index.

    Note that the 'name' exists on the 'p' tag
    Here's the error in FF:
    Code:
    document.getElementsByTagName("p")[0].name is undefined
    And here's the code that's throwing the error:
    Code:
    var j = 0;
    for (i = 0; i < document.getElementsByTagName("p").length; i++) {
      // error occurs on this line
      if(document.getElementsByTagName("p")[0].name.substr(0,4) == "revp"){
        document.getElementById("revp" + (j++)).innerHTML = '';
      }
    }


Bookmarks

Posting Permissions

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