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 = 
    // 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");
    // ... compose the rest document content ...
    // use specific Xerces class to write DOM-data to a file:
    XMLSerializer serializer = new XMLSerializer();

    } catch (Exception ex) {