XML
  Home arrow XML arrow Page 4 - XML Parsing With DOM and Xerces (part 1)
Dev Shed Forums  
Administration  
AJAX  
Apache  
BrainDump  
DHTML  
Flash  
Java  
JavaScript  
Multimedia  
MySQL  
Oracle  
Perl  
PHP  
Practices  
Python  
Reviews  
Security  
Smartphone Development  
Style-Sheets  
Web Services  
XML  
Zend  
Zope  
Mobile Linux  
App Generation ROI  
IBM® developerWorks  
Forums Sitemap  
E-Commerce Hosting  
Linux Web Hosting  
Managed Hosting  
Small Business Hosting  
VPS Hosting  
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? 
Google.com  
XML

XML Parsing With DOM and Xerces (part 1)
By: icarus, (c) Melonfire
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: starstarstarstarstar / 39
    2002-02-19


    Table of Contents:
  • XML Parsing With DOM and Xerces (part 1)
  • Float Like A Butterfly...
  • Nailguns, Going Cheap
  • Delving Deeper
  • When Laziness Is A Virtue

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      error-file:tidyout.log Del.ici.ous error-file:tidyout.log Digg
      error-file:tidyout.log Blink error-file:tidyout.log Simpy
      error-file:tidyout.log Google error-file:tidyout.log Spurl
      error-file:tidyout.log Y! MyWeb error-file:tidyout.log 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


    XML Parsing With DOM and Xerces (part 1) - Delving Deeper
    ( Page 4 of 5 )

    As you must have figured out by now, using the DOM parser is fairly easy - essentially, it involves creating a "tree" of the elements in the XML document, and traversing that tree with built-in methods. In the introductory example, I ventured as far as the document element; in this next one, I'll go much further, demonstrating how the parser's built-in methods can be used to navigate to any point in the document tree.

    import org.apache.xerces.parsers.DOMParser; import org.w3c.dom.*; import java.io.*; public class MySecondDomApp { // constructor public MySecondDomApp (String xmlFile) { // create a Xerces DOM parser DOMParser parser = new DOMParser(); // parse the document and // access the root node with its children try { parser.parse(xmlFile); Document document = parser.getDocument(); NodeDetails(document); } catch (IOException e) { System.err.println (e); } } // this function drills deeper into the DOM tree private void NodeDetails (Node node) { // get the node name System.out.println (node.getNodeName()); // check if the node has children if(node.hasChildNodes()) { // get the child nodes, if they exist NodeList children = node.getChildNodes(); if (children != null) { for (int i=0; i< children.getLength(); i++) { // repeat the process for each child node // get the node name System.out.println ("t" + children.item(i).getNodeName()); // check if the node has children if(children.item(i).hasChildNodes()) { // get the children, if they exist NodeList childrenOfchildren = children.item(i).getChildNodes(); if (childrenOfchildren != null) { // get the node name for (int j=0; j< childrenOfchildren.getLength(); j++) { System.out.println ("tt" + childrenOfchildren.item(j).getNodeName()); } } } } } } } // the main method to create an instance of our DOM application public static void main (String[] args) { MySecondDomApp MySecondDomApp = new MySecondDomApp (args[0]); } }
    Here's the output:

    #document inventory #text item #text item #text
    As demonstrated in the first example, the fundamentals remain unchanged - initialize the parser, read an XML document, get a reference to the root of the tree and start traversing the tree. Consequently, most of the code here remains the same as that used in the introductory example, with the changes occurring only in the NodeDetails() function. Let's take a closer look at this function:

    // this function drills deeper into the DOM tree private void NodeDetails (Node node) { // get the node name System.out.println (node.getNodeName()); // check if the node has children if(node.hasChildNodes()) { // get the child nodes, if they exist NodeList children = node.getChildNodes(); if (children != null) { for (int i=0; i< children.getLength(); i++) { // repeat the process for each child node // get the node name System.out.println ("t" + children.item(i).getNodeName()); // check if the node has children if(children.item(i).hasChildNodes()) { // get the children, if they exist NodeList childrenOfchildren = children.item(i).getChildNodes(); if (childrenOfchildren != null) { // get the node name for (int j=0; j< childrenOfchildren.getLength(); j++) { System.out.println ("tt" + childrenOfchildren.item(j).getNodeName()); } } } } } } }
    Once a reference to the root of the tree has been obtained and passed to NodeDetails(), the getChildNodes() function is used to obtain a list of the children of that node. This list is returned as a new NodeList object, which comes with its own methods for accessing individual elements of the node list.

    As you can see, one of these methods is the getLength() method, used to obtain the number of child nodes, in order to iterate through them. Individual elements of the node list can be accessed via the item() method, which returns a Node object, which puts us back on familiar territory - the Node object's standard getNodeName() and getNodeType() methods can now be used to access detailed information about the node.

    The process is then repeated for each of these Node objects - a check for further children, a retrieved NodeList, a loop iterating through the child Nodes - until the end of the document tree is reached.

     
     
    >>> More XML Articles          >>> More By icarus, (c) Melonfire
     

       

    XML ARTICLES

    - Flex Array Collection Sort and Filtering
    - The Flex Tree Control
    - Flex List Controls
    - Working with Flex and Datagrids
    - How to Set Up Podcasting and Vodcasting
    - Creating an RSS Reader Application
    - Building an RSS File
    - An Introduction to XUL Part 6
    - An Introduction to XUL Part 5
    - An Introduction to XUL Part 4
    - An Introduction to XUL Part 3
    - An Introduction to XUL Part 2
    - An Introduction to XUL Part 1
    - XML Matters: Practical XML Data Design and M...
    - Practical XML Data Design and Manipulation f...





    © 2003-2009 by Developer Shed. All rights reserved. DS Cluster 1 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek