Roaming through XMLDOM: An AJAX Prerequisite - Continuing the Dissection
(Page 3 of 4 )
Document Element Properties
The Document element has a child nodes collection you can access by iterating through the index. You can also access the attributes, which consist of a name and a value as shown here. The following code should be inserted after creating the XMLDOC object.
document.write("<br>"+xdoc.documentElement.childNodes.item(0).
text +"<br>");
document.write("<br>"+xdoc.documentElement.childNodes.item(1).
attributes[0].name +"<br>");
document.write("<br>"+xdoc.documentElement.childNodes.item(1).
attributes[0].value +"<br>");
When the above snippet is run you would see the following.
My students who took web programming class with me
id
1
Nodes Collection
In the document we are looking at, we have a number of student nodes. We can get a reference to the student nodes using the getElementsByTagName method. From this we can find the number of such student nodes. Once we know how many there are (nodes.length), we can iterate through the collection and find the individuals as shown in the following code.
var nodes=xdoc.getElementsByTagName("student");
document.write("<br>Number of student nodes is "+nodes.length+"<br>");
for (i=0; i< nodes.length; i++)
{document.write("<br>"+nodes.item(i).text+"<br>");}
If you were to include the code in the script after instantiating the xdoc object you would see the following:
Number of student nodes is 4
Linda Jones Access, VB5.0
Adam Davidson Cobol, MainFrame
Charles Boyer HTML, Photoshop
Charles Mann Cobol, MainFrame
Node's name, type and value
Once you locate an element's child node through its collection, as in the declaration of Elem in the next snippet, you could determine its name, type and value by calling respectively the nodeName, nodeType and nodeValue properties as shown.
var Elem=xdoc.documentElement.childNodes.item(2);
document.write("<br>"+Elem.nodeName+"<br>");
document.write("<br>"+Elem.nodeType+"<br>");
document.write("<br>"+Elem.nodeValue+"<br>");
We have located the second child and its properties by running the above script as shown.
student
1
null
Family metaphor related items
As mentioned earlier, in addition to the tree representation (tree metaphor) there is also the family representation (somewhat seniority based). These are usually parent, sibling, previous sibling, next sibling, and so on. The next few lines of code will give you an idea how we may access them. We will be starting with the second student whose id=2.
//second student is referenced.
var midElem = xdoc.documentElement.childNodes.item(2);
document.write("<br>"+midElem.attributes[0].value+"<br>");
document.write("<br>"+midElem.previousSibling.text+"<br>");
document.write("<br>"+midElem.nextSibling.text+"<br>");
document.write("<br>"+midElem.parentNode.nodeName+"<br>");
document.write("<br>"+midElem.parentNode.nodeType+"<br>");
document.write("<br>"+midElem.parentNode.nodeValue+"<br>");
When the above code is inserted after instantiating xdoc as described previously, you would see the following displayed.
2
Linda Jones Access, VB5.0
Charles Boyer HTML, Photoshop
wclass
1
null
Next: Manipulating the XML Document >>
More XML Articles
More By Jayaram Krishnaswamy