Roaming through XMLDOM: An AJAX Prerequisite - Getting at the Innards of an XML Document
(Page 2 of 4 )
We will briefly look at some of the XML Dom objects. In order to show how the code accesses the various parts of a document, consider the following well formed XML document WebClass.xml which is located on my hard drive at:
C:/Documents and settings/computer user/Desktop/todo/Ajax/WebClass.xml.
XML document probed
<?xml version="1.0" encoding="ISO-8859-1" ?>
<wclass>
<!-- My students who attended my web programming class -->
<student id="1">
<name>Linda Jones</name>
<legacySkill>Access, VB5.0</legacySkill>
</student>
<student id="2">
<name>Adam Davidson</name>
<legacySkill>Cobol, MainFrame</legacySkill>
</student>
<student id="3">
<name>Charles Boyer</name>
<legacySkill>HTML, Photoshop</legacySkill>
</student>
<student id="4">
<name>Charles Mann</name>
<legacySkill>Cobol, MainFrame</legacySkill>
</student>
</wclass>
Slicing the XML document
First things first: we need to instantiate an XMLDocument object. This is done easily by running the following code as shown:
<SCRIPT LANGUAGE="JavaScript">
var xml_doc = new ActiveXObject("Microsoft.XMLDOM");
xml_doc.async = false;
xml_doc.load ("C:/Documents and settings/computer user/Desktop/
todo/Ajax/WebClass.xml");
</SCRIPT>
After this step we will look at a number of properities that can be accessed using code.
First Child and Last Child Properties
These properties can be accessed if you insert the following lines after loading the document as shown above but before </Script>,
document.write ("This documents first child is a " +
xml_doc.documentElement.firstChild.nodeName);
document.write("<br>");
document.write ("This document's last child= " +
xml_doc.documentElement.lastChild.nodeName);
and if you display it in a browser you would see the following:
This documents first child is a #comment
This document's last child= student