XML
  Home arrow XML arrow Page 3 - Roaming through XMLDOM: An AJAX Prerequisi...
Dev Articles Forums 
ADO.NET  
Apache  
ASP  
ASP.NET  
C#  
C++  
ColdFusion  
COM/COM+  
Delphi-Kylix  
Design Usability  
Development Cycles  
DHTML  
Embedded Tools  
Flash  
Graphic Design  
HTML  
IIS  
Interviews  
Java  
JavaScript  
MySQL  
Oracle  
Photoshop  
PHP  
Reviews  
Ruby-on-Rails  
SQL  
SQL Server  
Style Sheets  
VB.Net  
Visual Basic  
Web Authoring  
Web Services  
Web Standards  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
Sun Developer Network 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
XML

Roaming through XMLDOM: An AJAX Prerequisite
By: Jayaram Krishnaswamy
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 9
    2006-02-20

    Table of Contents:
  • Roaming through XMLDOM: An AJAX Prerequisite
  • Getting at the Innards of an XML Document
  • Continuing the Dissection
  • Manipulating the XML Document

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    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
    

    More XML Articles
    More By Jayaram Krishnaswamy


       · Object model is the most important thing. Once you know the object, you know a lot...
       · for bringing this all together in one place. You saved readers tons of time that...
       · Thanks for the kind comments. This is what you will find in most of my tutorials,...
     

    XML ARTICLES

    - 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
    - Simple Web Syndication with RSS 2.0
    - Java UI Design with an IDE
    - UI Design with Java and XML Toolkits
    - Displaying ADO Retrieved Data with XML Islan...
    - Widget Walkthrough
    - Introduction to Widgets
    - The Why and How of XML Data Islands






    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 2 hosted by Hostway
    Stay green...Green IT