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:


xmlDoc.getElementsByTagName("CATALOG")[0].childNodes[0].nodeValue;

Here’s the html:


<!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:


<?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>

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

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);

}

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.

It works!! Thanks again TommiChi.

no problem. glad it helped

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:


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:


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:


<?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>


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?

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:


document.getElementsByTagName("p")[0].name is undefined

And here’s the code that’s throwing the error:


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 = '';
  }
}