Java XML Document to File writeDOM2XMLFile(Document domDoc, String fileName)

Here you can find the source of writeDOM2XMLFile(Document domDoc, String fileName)

Description

write the dom content into xml file

License

Apache License

Parameter

Parameter Description
indent a parameter
n a parameter

Declaration

public static void writeDOM2XMLFile(Document domDoc, String fileName) 

Method Source Code

//package com.java2s;
/** //from   ww w . ja  v  a2  s . c o m
 * UIMA Common
 * Copyright (C) 2010  Nicolas Hernandez
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an
 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
 * KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations
 * under the License.
 */

import java.io.File;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;

public class Main {
    /**
     *  write the dom content into xml file
     * @param indent
     * @param n
     */
    public static void writeDOM2XMLFile(Document domDoc, String fileName) {
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = null;
        try {
            transformer = transformerFactory.newTransformer();
        } catch (TransformerConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        // no effect ?
        //      transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        //      transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8");
        //      transformer.setOutputProperty(OutputKeys.STANDALONE , "yes");
        //      transformer.setOutputProperty(OutputKeys.INDENT, "yes");

        DOMSource source = new DOMSource(domDoc);
        StreamResult result = new StreamResult(new File(fileName));

        // Output to console for testing
        // StreamResult result = new StreamResult(System.out);

        try {
            transformer.transform(source, result);
        } catch (TransformerException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        System.err.println("Info: dom document saved in file, " + fileName + " !");
    }
}

Related

  1. writeDocument(Document document, File file)
  2. writeDocument(Document document, File file)
  3. writeDocument(Document document, String path)
  4. writeDocumentTo(Document doc, File file)
  5. writeDocumentToFile(Document document, String filePathname, String method, int indent)
  6. writeDOMDocumentToFile(Document doc, String filePath)
  7. writeDomToFile(Document doc, String filename)
  8. writeDomToFile(Document dom, File outFile)
  9. writeDomToFile(Document dom, File outFile)