Java XML Document to String write(Document doc)

Here you can find the source of write(Document doc)

Description

write

License

Open Source License

Declaration

public static String write(Document doc) throws Exception 

Method Source Code


//package com.java2s;
/*//from w w  w  . j a  v  a 2 s  .  c om
 * See the NOTICE file distributed with this work for additional
 * information regarding copyright ownership.
 *
 * This 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 2.1 of
 * the License, or (at your option) any later version.
 *
 * This software 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 this software; if not, write to the Free
 * Software Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA
 * 02110-1301 USA, or see the FSF site: http://www.fsf.org.
 */

import java.io.ByteArrayOutputStream;

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

import java.io.Writer;

import javax.xml.transform.Result;
import javax.xml.transform.Source;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
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.sax.SAXSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;

import org.xml.sax.InputSource;
import org.xml.sax.XMLReader;

public class Main {
    public static String write(Document doc) throws Exception {
        return write(doc.getDocumentElement());
    }

    public static String write(Element e) throws Exception {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        write(e, baos);
        return new String(baos.toByteArray(), "UTF-8");
    }

    public static void write(Element root, OutputStream os) throws Exception {
        Writer writer = new OutputStreamWriter(os);
        write(root, writer);
    }

    /**
     * @param root
     * @param writer
     * @throws TransformerConfigurationException
     * @throws TransformerFactoryConfigurationError
     * @throws TransformerException
     */
    public static void write(Element root, Writer writer)
            throws TransformerConfigurationException, TransformerFactoryConfigurationError, TransformerException {
        Source input = new DOMSource(root);
        write(input, writer);
    }

    /**
     * @param reader
     * @param parser
     * @param output
     * @throws TransformerConfigurationException
     * @throws TransformerFactoryConfigurationError
     * @throws TransformerException
     */
    public static void write(Reader reader, XMLReader parser, Result output)
            throws TransformerConfigurationException, TransformerFactoryConfigurationError, TransformerException {
        SAXSource input = new SAXSource();
        InputSource inputSource = new InputSource();
        inputSource.setCharacterStream(reader);
        input.setInputSource(inputSource);
        input.setXMLReader(parser);
        write(input, output);
    }

    /**
     * @param parser
     * @param writer
     * @throws TransformerConfigurationException
     * @throws TransformerFactoryConfigurationError
     * @throws TransformerException
     */
    public static void write(Reader reader, XMLReader parser, Writer writer)
            throws TransformerConfigurationException, TransformerFactoryConfigurationError, TransformerException {
        Result output = new StreamResult(writer);
        write(reader, parser, output);
    }

    public static void write(Source input, Result output)
            throws TransformerConfigurationException, TransformerFactoryConfigurationError, TransformerException {
        write(input, output, true);
    }

    public static void write(Source input, Result output, boolean indent)
            throws TransformerConfigurationException, TransformerFactoryConfigurationError, TransformerException {
        boolean omitxmldeclaration = true;
        Transformer idTransform = TransformerFactory.newInstance().newTransformer();
        if (omitxmldeclaration) {
            idTransform.setOutputProperty("omit-xml-declaration", "yes");
        }
        idTransform.setOutputProperty("encoding", "UTF-8");
        if (indent) {
            idTransform.setOutputProperty("indent", "yes");
            try {
                idTransform.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");
            } catch (Exception e) {
                //
            }
        }
        idTransform.transform(input, output);
    }

    /**
     * @param input
     * @param os
     * @throws TransformerConfigurationException
     * @throws TransformerFactoryConfigurationError
     * @throws TransformerException
     */
    public static void write(Source input, Writer writer)
            throws TransformerConfigurationException, TransformerFactoryConfigurationError, TransformerException {
        Result output = new StreamResult(writer);
        write(input, output);
    }
}

Related

  1. toXml(Document domDoc)
  2. toXmlString(Document doc)
  3. toXmlString(Document doc)
  4. toXMLString(Document doc, boolean includeXMLDecl, boolean indent)
  5. toXmlString(Document document)
  6. write(Document doc, Result result)
  7. write(String filename, Document document, boolean addDocType)
  8. write2xml(Document document)
  9. write2Xml(Document document)