java left logo
java middle logo
java right logo
 

Home arrow Java SE Tips arrow org.w3c.dom arrow Accessing different types of DOM tree nodes
 
 
Main Menu
Home
Java Tutorials
Book Reviews
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Java Network
Java Forums
Java Blog




Most Visited Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews
Top Rated Tips
Java SE Tips
Java ME Tips
Java EE Tips
Other API Tips
Java Applications
Java Libraries
Java Games
Book Reviews


Statistics
Registered Users: 4093
Java SE Tips: 614
Java ME Tips: 202
Java EE Tips: 183
Other API Tips: 779
Java Applications: 298
Java Libraries: 209
Java Games: 16
Book Reviews:
 
 
 
Accessing different types of DOM tree nodes E-mail
User Rating: / 3
PoorBest 

As it was already mentioned DOM-document contains the only root element. The root in its turn consists of nodes. XML-element are concrete types of those node. Except elements nodes can be also comments, text, attribute, notation, processing instruction and event the document it self. Since when we request a child from an element we get a node of unknown type, we may use its getNodeValue() method to indicates its type and to cast to corresponding interface:

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;

import org.w3c.dom.*;

/**
 * This sample program shows how to browse through DOM-tree,
 * determine the types of DOM-nodes and printing their 
 * specific contents.
 */

public class Test {
  public static void main(String[] args) {
    try {
      // first of all we request out 
      // DOM-implementation:
      DocumentBuilderFactory factory = 
        DocumentBuilderFactory.newInstance();
      // then we have to create document-loader:
      DocumentBuilder loader = factory.newDocumentBuilder();

      // loading a document...
      Document document = loader.parse("sample.xml");
      
      // access to root element:
      Element purchaseOrder = document.getDocumentElement();
      
      // print a document element content:
      printElement(purchaseOrder, "");
            
    catch (Exception ex) {
      ex.printStackTrace();
    }
  }
  
  public static final void printElement(Element element, String indent) {
    System.out.println("Element '" + element.getNodeName() "'");
    
    NodeList children = element.getChildNodes();
    for (int i = 0; i < children.getLength(); i++) {
      Node child = children.item(i);
      
      switch (child.getNodeType()) {
      case Node.ELEMENT_NODE:
        // recursive call for all element children:
        printElement((Elementchild, indent + "\t");
        break;
      case Node.ATTRIBUTE_NODE:
        Attr attr = (Attrchild;
        System.out.println("\tAttribute: '" 
          attr.getName() "' = '" + attr.getValue() "'");
        break;
      case Node.COMMENT_NODE:
        Comment comment = (Commentchild;
        System.out.println("\tComment: '" + comment.getData() "'");
        break;
      case Node.CDATA_SECTION_NODE:
        CharacterData cdata = (CharacterDatachild;
        System.out.println("\tCDatat: '" + cdata.getData() "'");
        break;
      case Node.TEXT_NODE:
        Text text = (Textchild;
        System.out.println("\tText: '" + text.getData() "'");
        break;
      // ... all cases remaining ...
      default:
        System.out.println("\tUnknown node type: '" 
          + child.getNodeType() "'");
        break;
      }
    }
  }
}


 Related Tips

 
< Prev   Next >

Page 1 of 0 ( 0 comments )

You can share your information about this topic using the form below!

Please do not post your questions with this form! Thanks.


Name (required)


E-Mail (required)

Your email will not be displayed on the site - only to our administrator
Homepage(optional)



Comment Enable HTML code : Yes No



 
       
         
     
 
 
 
   
 
 
java bottom left
java bottom middle
java bottom right
RSS 0.91 FeedRSS 1.0 FeedRSS 2.0 FeedATOM FeedOPML Feed

Home - About Us - Privacy Policy
Copyright 2005 - 2008 www.java-tips.org
Java is a trademark of Sun Microsystems, Inc.