Convert an HTTP responseText into a DOM document object

  • Thread starter Water Cooler v2
  • Start date
W

Water Cooler v2

I send an XmlHttpRequest and GET a response back. Is it possible to
convert that response into a DOM Level 3 HTML 'document' object and
investigate it?
 
M

Martin Honnen

Water said:
I send an XmlHttpRequest and GET a response back. Is it possible to
convert that response into a DOM Level 3 HTML 'document' object and
investigate it?

The response is parsed into a DOM document if the server sends it as
text/xml or application/xml. Then the responseXML property is populated
as a DOM document. Whether that is DOM Level 3 document depends on the
browser (Opera 9 has some DOM Level 3 support).
I don't think any browser currently allows you to take a string (e.g.
responseText) and parse it into a HTML DOM document, unless you
document.wrote it to a frame window.
 
T

Thomas 'PointedEars' Lahn

Martin said:
The response is parsed into a DOM document if the server sends it as
text/xml or application/xml. Then the responseXML property is populated
as a DOM document. Whether that is DOM Level 3 document depends on the
browser (Opera 9 has some DOM Level 3 support).

As have Gecko-based UAs since a while.

http://developer.mozilla.org/en/docs/DOM_Levels#DOM_Level_3
I don't think any browser currently allows you to take a string (e.g.
responseText) and parse it into a HTML DOM document, unless you
document.wrote it to a frame window.

Your own FAQ entry says that it is possible to parse the response into an
X(HT)ML document with MSXML and with Gecko's DOMParser::parseFromString(),
which could suffice here:

http://www.faqts.com/knowledge_base/entry/versions/index.phtml?aid=15302


PointedEars
 
D

Darko

I send an XmlHttpRequest and GET a response back. Is it possible to
convert that response into a DOM Level 3 HTML 'document' object and
investigate it?

Collecting different pieces of code for a while, removing good from
bad etc.,
now I use the following function which I hope (and think) you'll find
of use. Just
hand it the ajax object (after it had sent the request and the
readyState had been
4), and you'll be returned the document. If I copied it correctly,
then it will work like
it does on my site, and that is - supporting Opera, IE and Mozilla.

function getXMLDocument( ajax )
{
if (typeof DOMParser == "undefined") {
DOMParser = function()
{};

DOMParser.prototype.parseFromString = function(str, contentType)
{
if (typeof ActiveXObject != "undefined") {
var doc = new ActiveXObject("MSXML.DomDocument");
doc.loadXML(str);
return doc;
} else if ( typeof XMLHttpRequest != "undefined" ) {
var req = new XMLHttpRequest();
req.open("GET", "data:" + (contentType || "application/xml") +
";charset=utf-8," + encodeURIComponent(str), false);
if ( req.overrideMimeType )
req.overrideMimeType(contentType);
req.send(null);
return req.responseXML;
} else
throw new FatalException( "Can't find a valid xml parser",
"AJAX::getXMLDocument()" );
}
}
var strDocument = ajax.responseText;
var xmlDocument = ajax.responseXML;
try {
if( ! xmlDocument || xmlDocument.childNodes.length === 0 )
xmlDocument = (new DOMParser()).parseFromString( strDocument,
"application/xml" );
return xmlDocument;
} catch( e ) {
return null;
}
}

Regards
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,579
Members
45,053
Latest member
BrodieSola

Latest Threads

Top