java left logo
java middle logo
java right logo
 

Home arrow Java SE Tips arrow org.w3c.dom arrow Reading a DOM tree from XML document
 
 
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: 4096
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:
 
 
 
Reading a DOM tree from XML document E-mail
User Rating: / 27
PoorBest 

DOM is a high-level API for processing XML data. DOM uses SAX to analyze the source. The difference from SAX is that there is no need to handle XML element directly. Entire document is composed and returned by a DocumentBuilder instance. Sample below shows ordinary loading of XML using DOM:

import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.FactoryConfigurationError;
import javax.xml.parsers.ParserConfigurationException;
import org.xml.sax.SAXException;

import org.w3c.dom.*;

/**
 * This sample program shows how to load XML data
 * using DOM interface.
 */

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 DOM-tree...
      Document document = loader.parse("sample.xml");
      // at last, we get a root element:
      Element tree = document.getDocumentElement();

      // ... do something with document element ...

    catch (IOException ex) {
      // any IO errors occur:
      handleError(ex);
    catch (SAXException ex) {
      // parse errors occur:
      handleError(ex);
    catch (ParserConfigurationException ex) {
      // document-loader cannot be created which,
      // satisfies the configuration requested
      handleError(ex);
    catch (FactoryConfigurationError ex) {
      // DOM-implementation is not available 
      // or cannot be instantiated:
      handleError(ex);
    }
  }

  private static final void handleError(Throwable ex) {
    // ... handle error here...
  }
}


 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.