hey. I'm trying to get a grasp on XML for use with AJAX.
here is my code. what i want to do is grab the stuff between the 'content' tags, and display it. then i want to take the stuff between the 'script' tags, and eval it (so it will execute).

here is my simple xml document:

<?xml version="1.0" encoding="ISO-8859-1"?>
<document>
<myScript> alert("hello world"); </myScript>
<content> This text should be displayed as content. </content>
</document>

and my handleResponse code from the calling page:

function handleResponse(http_request, divID){
   var myDiv = document.getElementById(divID);
   if (http_request.readyState == 4) {
      if (http_request.status == 200) {
          myXMLDoc = http_request.responseXML;
          content = myXMLDoc.getElementsByTagName("content");
          myDiv.innerHTML = content; 
   } else {
   alert("There was a problem with the request.");
  }
 }
}

When i run this, i get the error "object doesn't support this property or method" ... and its complaining about the line :
content = myXMLDoc.getElementsByTagName("content");

Can anybody see what I'm doing wrong, and possibly suggest how to fix it? I'm just getting into XML, and it would be a big help
Thanks!

Dani AI

Generated

The error you saw happens when getElementsByTagName is called on something that is not an XML document (typically because responseXML is null). As pointed out, the XML needs to be parsed — the browser will only populate responseXML if the response is valid XML and the server sends an XML MIME type. Common causes: wrong Content-Type, stray characters (BOM or whitespace) before the <?xml?> prolog, malformed XML, or cross-domain/local-file restrictions.

Quick diagnostics: open the browser Network tab and examine the response body and the Content-Type header (should be application/xml or text/xml). In the console check xhr.responseXML and xhr.responseText to see whether an XML document was produced or if a parser error/node appears. See the XMLHttpRequest responseXML notes on MDN for details (XMLHttpRequest.responseXML).

If responseXML is null, parse the text with DOMParser. When extracting content, pick the first content node and read its text (do not assign a NodeList to innerHTML). To execute script text from the XML, create a script element and append it so the browser executes it (safer than eval). Example pattern:

var doc = xhr.responseXML || new DOMParser().parseFromString(xhr.responseText, "application/xml");
var c = doc.getElementsByTagName("content")[0];
div.innerHTML = c ? (c.textContent || c.firstChild.nodeValue) : "";
var sNode = doc.getElementsByTagName("myScript")[0];
if (sNode) {
  var s = document.createElement("script");
  s.text = sNode.textContent || sNode.firstChild.nodeValue;
  document.head.appendChild(s);
}

Notes: XML tag names are case-sensitive, sanitize any script before running it, and prefer setting correct server headers so responseXML is filled automatically. For parsing details and textContent, see MDN: DOMParser and Node.textContent.

Your getting that error because that property does not exist on the XML document.

You must first parse the document before you can access the DOM. Once you do this your code should work.

Here is some info on parsing an XML document:

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.