Java XML String Transform save(Node doc, OutputStream stream, String encoding, boolean indent)

Here you can find the source of save(Node doc, OutputStream stream, String encoding, boolean indent)

Description

save

License

Open Source License

Declaration

private static void save(Node doc, OutputStream stream, String encoding, boolean indent) throws IOException 

Method Source Code


//package com.java2s;
/* /*from  w w w .  j  av a2 s .  c o  m*/
* This file is part of DISMOD Core. 
* ? Copyright 2003-2011 Fraunhofer Institut f. Materialfluss und Logistik.
* http://www.iml.fraunhofer.de, http://www.verkehrslogistik.de
*
* DISMOD Core is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* 
* DISMOD Core 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 Lesser General Public License for more details.
* 
* You should have received a copy of the GNU Lesser General Public License
* along with DISMOD Core.  If not, see <http://www.gnu.org/licenses/>.
*/

import java.io.File;

import java.io.FileOutputStream;
import java.io.IOException;

import java.io.OutputStream;

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

import org.w3c.dom.Node;

public class Main {
    private static void save(Node doc, OutputStream stream, String encoding, boolean indent) throws IOException {
        save(doc, stream, encoding, indent, null, null);
    }

    private static void save(Node doc, OutputStream stream, String encoding, boolean indent, String publicDocType,
            String systemDocType) throws IOException {

        try {
            Transformer transformer = TransformerFactory.newInstance().newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, indent ? "yes" : "no");
            transformer.setOutputProperty(OutputKeys.ENCODING, encoding);
            if (publicDocType != null)
                transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, publicDocType);
            if (systemDocType != null)
                transformer.setOutputProperty(OutputKeys.DOCTYPE_SYSTEM, systemDocType);

            StreamResult result = new StreamResult(stream);
            DOMSource source = new DOMSource(doc);
            transformer.transform(source, result);
        } catch (Exception e) {
            throw new IOException("Failure while writing dom result.", e);
        }
    }

    @SuppressWarnings("resource")
    public static void save(Node doc, File file) throws IOException {
        save(doc, new FileOutputStream(file), "ISO-8859-1", true);
    }

    public static void save(Node doc, OutputStream stream) throws IOException {
        save(doc, stream, "ISO-8859-1", true);
    }

    public static void save(Node doc, OutputStream stream, String encoding) throws IOException {
        save(doc, stream, encoding, true);
    }

    public static void save(Node doc, OutputStream stream, String encoding, String publicDocType,
            String systemDocType) throws IOException {
        save(doc, stream, encoding, true, publicDocType, systemDocType);
    }
}

Related

  1. readFile(String systemId)
  2. readURL(String urlStr)
  3. readXMLFile(String xmlFileName)
  4. render(String name, byte[] xmldata)
  5. replaceLineSeparatorInternal(String string, String lineSeparator)
  6. signEmbeded(Node doc, String uri, PrivateKey pKey, X509Certificate cert)
  7. String2Doc(String InputXMLString)
  8. string2Source(String xml)
  9. stringToNode(String s)