java left logo
java middle logo
java right logo
 

Home arrow Java SE Tips
 
 
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: 4101
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:
 
 
 
Creating a new DOM tree E-mail
User Rating: / 18
PoorBest 

Since DOM is a document model, DOM-document may be created directly, without loading from a XML file. Though, in order to create new DOM document we have to use the same DocumentBuilder. After document is created it should be supplied with document (root) element. All the elements a creates via method of class Document:

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

import org.w3c.dom.*;

/**
 * This sample program shows how to create new document 
 * from stretch 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();

      // createing a new DOM-document...
      Document document = loader.newDocument();
      
      // initially it has no root-element, ... so we create it:
      Element root = document.createElement("purchase-order");
      
      // we can add an element to a document only once,
      // the following calls will raise exceptions:
      document.appendChild(root);

      // ... compose the rest document content ...
      
    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.