O'Reilly logo

JavaScript & DHTML Cookbook by Danny Goodman

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Embedding XML Data

NN 6, IE 5(Win)

Problem

You want to reference XML document data to support script activities in the main page.

Solution

IE 5 or later for Windows and Netscape 6 or later allow you to create an invisible virtual document that holds raw XML data, which your scripts may then traverse using standard DOM node referencing methods and properties. Loading the XML data requires browser-specific handling, but once that occurs, you can reference the content and its node tree uniformly.

The following verifySupport( ) function loads an external XML document (URL passed as a parameter) into a global variable named xDoc and returns a Boolean value indicating whether the browser supports the capability:

var xDoc; // verify that browser supports XML features and load external .xml file function verifySupport(xFile) { if (document.implementation && document.implementation.createDocument) { // this is the W3C DOM way, supported so far only in NN6+ xDoc = document.implementation.createDocument("", "theXdoc", null); } else if (typeof ActiveXObject != "undefined") { // make sure real object is supported (sorry, IE5/Mac) if (document.getElementById("msxml").async) { xDoc = new ActiveXObject("Msxml.DOMDocument"); } } if (xDoc && typeof xDoc.load != "undefined") { // load external file (from same domain) xDoc.load(xFile); return true; } else { var reply = confirm("This example requires a browser with XML support, " + "such as IE5+/Windows or Netscape 6+.\n \nGo back to previous page?"); ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required