Loading XML into Gecko-based browsers

Most Web developers think of Microsoft Internet Explorer and either MSXML2 or MSXML3 when asked about client-side XML. But for client-side XML support, there is an alternative to MSXML, namely Gecko.

Mention client-side XML to a group of Web developers and the majority of them will think of Microsoft Internet Explorer and either MSXML2 or MSXML3. But when it comes to client-side XML support there is an alternate to MSXML, namely Gecko. Just because nobody is in your face touting the XML support features of Mozilla and Netscape doesn’t mean that the support doesn’t exist. In fact, Gecko supports XML, XSLT, XPath, XMLHTTP, SVG (Scalable Vector Graphics) and MathML. The only real lack in Gecko’s XML support is how few people know about it, but I intend to remedy that with my big mouth.

JavaScript and XML
Because Gecko doesn’t rely on non-standard extensions, like XML data islands, the only way to invoke Gecko’s XML magic is through the use of JavaScript (ECMAScript). One of the advantages of this use of JavaScript is that if there is a problem with getting one method to work there is usually another way to get the job done. An example of this is the relatively simple task of loading the XML document in the first place. Just while playing with Mozilla, I’ve found three ways to load an XML document without trying real hard.

Loading the XML document
The first way is the brute force method of building the document using the XML DOM to programmatically create the XML document’s node. The problem with this method, shown in Listing A, is that it isn’t very flexible. Each and every time there is a change to the document the JavaScript needs to be changed. While this can lead to job security, after a couple of years, who would want the job?

The second way to load an XML document is a lot more familiar, simply by using the XML DOM’s load method. This is a lot like the method that is used with Microsoft’s MSXML, but there are several differences. The first of these differences is the fact that Gecko doesn’t support either Microsoft’s async extension or readystate extension to the XML DOM. So, instead of JavaScript sitting around “twiddling its thumbs” waiting for the document to load or checking the readystate, Gecko uses an onload event handler. The onload event handler fires once the document is loaded and processing can proceed, as Listing B shows.

The final way to load an XML document is for those, like myself, who want JavaScript to “twiddle its thumbs” waiting for an XML document to load. Instead of using the load method, XMLHTTP is used to load the document as shown in Listing C. The result of this is a pretty good simulation of Microsoft’s async property.

What’s next?
Now that the XML document is loaded, regardless of the method used, the question is what to do with it. Because Gecko supports XSLT, one idea is to create XHTML on the fly, which would drastically increase the dynamic part of DHTML. Another possibility is to use SOAP to access Web services on your server. Basically the only limitation on what to do next is your imagination.

Editor's Picks