java left logo
java middle logo
java right logo
 

Home arrow Java SE Tips arrow org.w3c.dom arrow Saving a DOM tree to XML file javax.xml.parsers (JAXP)
 
 
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: 4100
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:
 
 
 
Saving a DOM tree to XML file javax.xml.parsers (JAXP) E-mail
User Rating: / 62
PoorBest 

One of main DOM disadvantages is that it does not contain means to save created or modified documents. Thus, if we want to create new DOM-contents and save it to a file we have to write own serializer. Some XML packages like Xerces-J has such enhancement classes allowing to write DOM-tree to a file. But it is not defined in DOM standards. A sample below demonstrates how to use extended API to save DOM:

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

import org.apache.xml.serialize.XMLSerializer;

import org.w3c.dom.*;

/**
 * This sample program shows how to save new document
 * to a file using Xerces-J class XMLSerializer.
 */

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();

    // create root-element
    Element order = document.createElement("purchase-order");
    document.appendChild(order);
    // ... compose the rest document content ...
    
    // use specific Xerces class to write DOM-data to a file:
    XMLSerializer serializer = new XMLSerializer();
    serializer.setOutputCharStream(
      new java.io.FileWriter("order.xml"));
    serializer.serialize(document);

    catch (Exception ex) {
      ex.printStackTrace();
    }
  }
}


 Related Tips

 
< Prev   Next >
Posted by Juan Cruz, on Thursday, 29 June 2006 at 11:43

Actually you can, by using an identity transform:

          Document doc db.parse(...);                           TransformerFactory tf TransformerFactory.newInstance();                  Transformer t tf.newTransformer();                  t.transform(new DOMSource(doc), new StreamResult(System.out));


 1 
Page 1 of 1 ( 1 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.