JavaScript and XML

Most modern browsers already contain some tools to help you deal with XML documents.

A JavaScript object must exist to contain the XML document. Creating a new instance of such an object is done slightly differently depending on whether you use a non-Microsoft browser, such as Mozilla's Firefox, or Microsoft Internet Explorer:

For Firefox and other non-Microsoft browsers, use the following code to create a JavaScript XML document object:

<script type="text/javascript">
var myxmlDoc = document.implementation.createDocument("","",null);
myxmlDoc.load("exampleDoc.xml");

Program statements

</script>

 

To create a JavaScript XML document object with Internet Explorer, use this code:

<script type="text/javascript">
var myxmlDoc=new ActiveXObject("Microsoft.XMLDOM")
myxmlDoc.async="false"
myxmlDoc.load("exampleDoc.xml")

Program statements

</script>

 

After you have an object to represent the XML document, you may use the properties and methods of that object to gain access to the XML data contained within the document. Effectively, the hierarchical structure and data of the XML document now have equivalents in the JavaScript hierarchy of objects, the Document Object Model (DOM).