Saving a DOM tree to XML file javax.xml.parsers (JAXP) : XML Serialization « XML « Java Tutorial






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

import org.w3c.dom.Document;
import org.w3c.dom.Element;

import com.sun.org.apache.xml.internal.serialize.XMLSerializer;

public class Main {
  public static void main(String[] argv) throws Exception {
    DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

    DocumentBuilder loader = factory.newDocumentBuilder();

    Document document = loader.newDocument();

    Element order = document.createElement("order");
    document.appendChild(order);

    XMLSerializer serializer = new XMLSerializer();
    serializer.setOutputCharStream(new java.io.FileWriter("order.xml"));
    serializer.serialize(document);

  }
}








33.21.XML Serialization
33.21.1.Saving a DOM tree to XML file javax.xml.parsers (JAXP)
33.21.2.PropsToXML takes a standard Java properties file, and converts it into an XML file
33.21.3.Writing a DOM Document to an XML File
33.21.4.Writing Only the Text of a DOM Document
33.21.5.Create an XML document with DOM
33.21.6.Strip extra spaces in a XML string
33.21.7.Extracting an XML formatted string out of a DOM object