Java XML Document Print printDOM(File file, Document doc, String encoding)

Here you can find the source of printDOM(File file, Document doc, String encoding)

Description

print DOM

License

Open Source License

Declaration

public static void printDOM(File file, Document doc, String encoding) throws FileNotFoundException 

Method Source Code


//package com.java2s;
/*//w  w w . j a v  a2 s. c  o m
Copyright (C) 2009-2013 Bengt Martensson.
    
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 3 of the License, or (at
your option) any later version.
    
This program is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
General Public License for more details.
    
You should have received a copy of the GNU General Public License along with
this program. If not, see http://www.gnu.org/licenses/.
*/

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;

import java.io.OutputStream;

import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;

public class Main {
    public static void printDOM(OutputStream ostr, Document doc, String encoding) {
        try {
            Transformer tr = TransformerFactory.newInstance().newTransformer();
            if (encoding != null)
                tr.setOutputProperty(OutputKeys.ENCODING, encoding);
            tr.setOutputProperty(OutputKeys.INDENT, "yes");
            tr.setOutputProperty(OutputKeys.METHOD, "xml");
            tr.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
            tr.transform(new DOMSource(doc), new StreamResult(ostr));
        } catch (TransformerConfigurationException ex) {
            System.err.println(ex.getMessage());
        } catch (TransformerException ex) {
            System.err.println(ex.getMessage());
        }
    }

    public static void printDOM(File file, Document doc, String encoding) throws FileNotFoundException {
        printDOM(file != null ? new FileOutputStream(file) : System.out, doc, encoding);
        System.err.println("File " + file + " written.");
    }

    public static void printDOM(File file, Document doc) throws FileNotFoundException {
        printDOM(file, doc, null);
    }
}

Related

  1. printDocument(final Document doc)
  2. printDocument(Node _docOrNode, OutputStream _outStream)
  3. printDocument(Node doc)
  4. printDocumentToStdout(final Document document)
  5. printDocumentTree(Node el)
  6. printElements(Document doc)
  7. printElements(Document dom)
  8. printPI(Document doc, PrintStream pstrm)
  9. printXML(Document doc)