WEEK 9: DOCUMENT OBJECT MODEL |
| |
| Reading Assignments | |
|---|---|
| BOOK | PAGES |
| XML in easy steps | .CH 11 |
LINKS TO XML RELATED SITES
DOM, allows the users to access the data in a manner which is very similar to accessing a property of an object in object-oriented or object-based languages such as Java or JavaScript. What DOM specifies, is the mapping of the natural tree-like hierarchy of an XML document into a set of APIs (methods and properties) which interfaces the document.
IE5 does support the DOM standard specified by the W3C (World Wide Web Consortium) plus they have added some of their own. In order to get access to the DOM in IE5 we first need to obtain a reference to the XML document. To obtain the reference we first need to identify our XML islands by assigning to the "id" attribute of the XML tag. Second we need get the node inside the JavaScript portion of our page with "node = document.all("document_id").XMLDocument".
Once we have the reference to the XML document we can then traverse the document tree using DOM.
Please note, in this example we are only obtaining a reference to the node and nothing more.
To move one step down, or inside the "<XML>" in order to get to the actual XML document we need to reference the "firstChild" of the XML reference, "students_document_node.firstChild". In our example the XML processing instruction, "<?xml version="1.0"? >" is the first thing inside the "<XML>" tag therefore, "students_document_node.firstChild" is pointing to that.
To point to the root tag "<students>" we need to look (not further down) beside or at the "nextSibling" of the "<?xml version="1.0"? >", by "students_document_node.firstChild.nextSibling".
At this point, we have made a reference to "<students>". To traverse inside the "<students>" we need to get a reference to its "firstChild" by, "students_document_node.firstChild.nextSibling.firstChild" statement. The firstChild might be another tag or it might be data. In this case it is data. To obtain the value that is inside of the "<students>" we need to reference the "nodeValue" property of the "firstChild" of students, "students_document_node.firstChild.nextSibling.firstChild.nodeValue".
As you can see "James" was retrieve and was passed to the alert() method.
The method, "getElementsByTagName(elementName)" returns a nodeList. A nodeList, as its name implies is an object that contains a collection of nodes. The nodeList has several methods among them is "item(index)" that would return a node at that "index". Upon obtaining a node we can then traverse down to the "firstChild" of the node and then its "nodeValue", "students_document_node.getElementsByTagName("students").item(0).firstChild.nodeValue". The result is same as our third example with the exception that adding or removing of processing instruction would not affect the parsing of the document.
Keep in mind this property is not part of the DOM specification and therefore it is only supported by the IE5 browsers.
Since attributes are in many ways are like tags, they are practically treated as such. Once we obtain the NamedNodeMap object we can get to the individual attributes as "node" objects by calling the "item(index)", or "getNamedItem(attributeName)" methods.
In our example we have used "getNamedItem(attributeName)" method. Since this method returns a node object we had to go through the same procedure as we had done for other nodes to obtain its value, "class_attribute.firstChild.nodeValue".