Java Reference
In-Depth Information
Safari and Chrome do not support loading XML fi les into a DOM object in the way covered in this
section. Instead, you must use the XMLHttpRequest object to request the XML document from the
server. This object is covered in Chapter 14.
This code creates an empty DOM object by using the createDocument() method of the document
.implementation object. After the DOM object is created, use the load() method to load an XML
document; it is supported by Firefox and Opera as well.
Safari and Chrome support the createDocument() method, but they do not support the load()
method.
Much like IE, Firefox and Opera support the async property, which allows the fi le to be loaded asyn-
chronously or synchronously. The behavior of loading synchronously is the same in these browsers as
in IE. However, things change when you want to load a fi le asynchronously.
Not surprising, Firefox and Opera use a different implementation from IE when it comes to checking
the load status of an XML fi le. In fact, these browsers do not enable you to check the status with some-
thing like the readyState property. Instead, they expose an onload event handler that executes when
the fi le is loaded and the DOM object is ready to use.
function xmlDoc_load()
{
alert(“XML is loaded!”);
}
var xmlDoc = document.implementation.createDocument(“”,””,null);
xmlDoc.onload = xmlDoc_load;
xmlDoc.load(“myfile.xml”);
This code loads the fi ctitious fi le myfile.xml in asynchronous mode. When the load process completes,
the load event fi res and calls xmlDoc_load(), which then shows the text XML is loaded! to the user.
Retrieving an XML File (Cross-Browser)
As you can see, the different ways of creating XML DOM objects require you to seek a cross-browser
solution. You can easily do this with object detection to determine which browser is in use. In fact, you
can easily edit the createDocument() function to include Firefox and Opera support. Look at the
following code:
function createDocument()
{
var xmlDoc;
if (window.ActiveXObject)
{
var versions =
[
“Msxml2.DOMDocument.6.0”,
“Msxml2.DOMDocument.3.0”
];
Search WWH ::




Custom Search