Loading XML into a page with xmlHttpRequest

18 Jan 2005 21:48 - (6) comments

Using javascript to talk to the server can be useful for complex applications that need to make multiple small requests to the server and make it fast. By doing this you can keep most of the logic on the server where it belongs. So it can help prevent the complex client anti-pattern (even worse ishaving basicaly the same code on the client and server side).

I dabbled a little with a xmlhttprequest demo.

In the onclick I call the showFeed function and pass the xml and xsl files as arguments. This is what showFeed looks like:

function showFeed(xmlUrl, xslUrl) {
var feed = document.getElementById('feed');

//clear feed div
while(feed.hasChildNodes()){
feed.removeChild(feed.childNodes[0]);
}
//append new htmlfragment
feed.appendChild(getHtmlFragment(xmlUrl, xslUrl));
}

First it finds the div where the new content will be added. I called it 'feed'. Then it removes all the old content and adds the new content by calling getHtmlFragment.

function getHtmlFragment(xmlUrl, xslUrl) {
var xslStylesheet;
var xsltProcessor = new XSLTProcessor();
//load the xml file
var xmlSource = getResponseXml(xmlUrl).responseXML;
//load the xsl file into the xslt Processor
xslStylesheet = getResponseXml(xslUrl).responseXML;
xsltProcessor.importStylesheet(xslStylesheet);
return xsltProcessor.transformToFragment(xmlSource, document);
}

function getResponseXml(xmlUrl) {
var xmlHttp = new XMLHttpRequest();
xmlHttp.open("GET", xmlUrl, false);
xmlHttp.send(null);
return xmlHttp;
}

The function getHtmlFragment loads all XML content by calling getResponseXml and uses xsltProcessor to transform the XML to XHTML.

Using xmlHttpRequest() seems to be all the rage and we'll be seeing a lot more examples.

Comments

Add a TITLE element and a HTML 4.0 DOCTYPE ;-)

On 18 Jan 23:54 by Anne

Done! :)

On 19 Jan 10:04 by Petrik

Nice demo. You could make a whole feed-reader in an HTML page with this: keep the blogroll in an XML file, load it, then load the feeds it points to.

One problem is that the browser seems to hang until the feed is loaded. This is a known problem with a known solution, the 'proper' way to send a request is demonstrated at http://jibbering.com/2002/4/httprequest.html ...All XMLHttpRequest demos should use async mode and the onreadystatechange property.

(Btw, the comment preview seems to escape apostrophes without reason.)

On 19 Jan 11:26 by Rahul

And line breaks aren't converted automatically?

On 19 Jan 11:27 by Rahul

Thanks Rahul! I fixed both.

On 19 Jan 14:44 by Petrik

As you are transforming the data on the client you could make use of the ResponseHeader.

var modded = xmlhttp.getResponseHeader("Last-Modified")

That way you can check the requested data has not changed and prevent an unnecessary download and all the bandwidth,code,checking status etc. that goes with fetching the data from the server for each transform.

On 20 Jan 3:22 by dave

Comments have been closed.

Admin