Convert an HTTP responseText into a DOM document object

  • Follow


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?

0
Reply wtr_clr (74) 11/2/2007 4:42:16 PM

Water Cooler v2 wrote:
> 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.

-- 

	Martin Honnen
	http://JavaScript.FAQTs.com/
0
Reply Martin 11/2/2007 4:54:31 PM


Martin Honnen wrote:
> Water Cooler v2 wrote:
>> 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).

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
-- 
"Use any version of Microsoft Frontpage to create your site. (This won't
prevent people from viewing your source, but no one will want to steal it.)"
  -- from <http://www.vortex-webdesign.com/help/hidesource.htm>
0
Reply Thomas 11/8/2007 11:38:26 AM

On Nov 2, 5:42 pm, Water Cooler v2 <wtr_...@yahoo.com> wrote:
> 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

0
Reply Darko 11/9/2007 12:14:03 AM

3 Replies
866 Views

(page loaded in 0.067 seconds)

Similiar Articles:












7/21/2012 3:45:50 AM


Reply: