Home arrow XML arrow Page 3 - Roaming through XMLDOM: An AJAX Prerequisite

Roaming through XMLDOM: An AJAX Prerequisite

The twin tasks of the XMLHttp are handling the HTTP request, and then processing the XML response. The first one is easily done by writing the appropriate syntax for creating this object. This is what is accomplished in AJAX by invoking the new constructor for XMLHTTP object. This is but one of the objects of the XMLDOM -XML Document Object Model. In this article, we look at XMLDOM in some detail before calling the XML document in an AJAX rendition in a future article.

Author Info:
By: Jayaram Krishnaswamy
Rating: 5 stars5 stars5 stars5 stars5 stars / 19
February 20, 2006
  1. · Roaming through XMLDOM: An AJAX Prerequisite
  2. · Getting at the Innards of an XML Document
  3. · Continuing the Dissection
  4. · Manipulating the XML Document

print this article

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.

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
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++)

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);

We have located the second child and its properties by running the above script as shown.

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);

When the above code is inserted after instantiating xdoc as described previously, you would see the following displayed.

Linda Jones Access, VB5.0
Charles Boyer HTML, Photoshop

blog comments powered by Disqus

- Open XML Finally Supported by MS Office
- XML Features Added to Two Systems
- Using Regions with XSL Formatting Objects
- Using XSL Formatting Objects
- More Schematron Features
- Schematron Patterns and Validation
- Using Schematron
- Datatypes and More in RELAX NG
- Providing Options in RELAX NG
- An Introduction to RELAX NG
- Path, Predicates, and XQuery
- Using Predicates with XQuery
- Navigating Input Documents Using Paths
- XML Basics
- Introduction to XPath

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 

Developer Shed Affiliates


© 2003-2018 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials