Java XML Document Serialize serialize(final Document doc, final OutputStream os, final String encoding)

Here you can find the source of serialize(final Document doc, final OutputStream os, final String encoding)

Description

Serializes a DOM.

License

Apache License

Declaration

static void serialize(final Document doc, final OutputStream os, final String encoding)
        throws TransformerFactoryConfigurationError, TransformerException, IOException 

Method Source Code


//package com.java2s;
/*/*ww w  .  ja  va 2s  . co m*/
 * #%L
 * xcode-maven-plugin
 * %%
 * Copyright (C) 2012 SAP AG
 * %%
 * Licensed 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.
 * #L%
 */

import java.io.IOException;

import java.io.OutputStream;
import java.io.OutputStreamWriter;

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

import org.w3c.dom.Document;

public class Main {
    /**
     * Serializes a DOM. The OutputStream handed over to this method is not closed inside this method.
     */
    static void serialize(final Document doc, final OutputStream os, final String encoding)
            throws TransformerFactoryConfigurationError, TransformerException, IOException {
        if (doc == null)
            throw new IllegalArgumentException("No document provided.");

        if (os == null)
            throw new IllegalArgumentException("No output stream provided");

        if (encoding == null || encoding.isEmpty())
            throw new IllegalArgumentException("No encoding provided.");

        final TransformerFactory transformerFactory = TransformerFactory.newInstance();
        transformerFactory.setAttribute("indent-number", Integer.valueOf(2));
        final Transformer t = transformerFactory.newTransformer();

        t.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        t.setOutputProperty(OutputKeys.METHOD, "xml");
        t.setOutputProperty(OutputKeys.INDENT, "yes");
        t.setOutputProperty(OutputKeys.ENCODING, encoding);

        final OutputStreamWriter osw = new OutputStreamWriter(os, encoding);
        t.transform(new DOMSource(doc), new StreamResult(osw));
        osw.flush();
    }
}

Related

  1. serialize(Document document, File targetFile)
  2. serialize(Document document, OutputStream ostream)
  3. serialize(Document document, OutputStream out)
  4. serialize(Document document, String fileName)
  5. serialize(Document document, Writer writer)
  6. serialize(final Document document, final OutputStream out)
  7. serializeDocument(Document doc)
  8. serializeDocument(Document doc, String filePath)
  9. serializeDocument(Document document)