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