The Java Developers Almanac 1.4


Order this book from Amazon.

   
Home > List of Packages > javax.xml.transform  [5 examples]

e518. Writing a DOM Document to an XML File

    // This method writes a DOM document to a file
    public static void writeXmlFile(Document doc, String filename) {
        try {
            // Prepare the DOM document for writing
            Source source = new DOMSource(doc);
    
            // Prepare the output file
            File file = new File(filename);
            Result result = new StreamResult(file);
    
            // Write the DOM document to the file
            Transformer xformer = TransformerFactory.newInstance().newTransformer();
            xformer.transform(source, result);
        } catch (TransformerConfigurationException e) {
        } catch (TransformerException e) {
        }
    }

 Related Examples
e519. Emitting a DOCTYPE Declaration When Writing an XML File from a DOM Document
e520. Writing Only the Text of a DOM Document


© 2002 Addison-Wesley.