Java XML Document to String writeDocument(Document doc, boolean omitXmlDeclaration)

Here you can find the source of writeDocument(Document doc, boolean omitXmlDeclaration)

Description

Writes a document as a string.

License

Open Source License

Parameter

Parameter Description
doc the document
omitXmlDeclaration true to omit the XML declaration

Exception

Parameter Description
Exception an exception

Return

the written document, or null if the conversion failed

Declaration

public static String writeDocument(Document doc, boolean omitXmlDeclaration) throws Exception 

Method Source Code

//package com.java2s;
/****************************************************************************
 *
 * Copyright (c) 2005-2012, Linagora/* w  w  w  .j av a  2  s .co m*/
 *
 * This library 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 library 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 library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 *
 *****************************************************************************/

import java.io.StringWriter;

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.Document;

public class Main {
    /**
    * Writes a document as a string.
    * @param doc the document
    * @param omitXmlDeclaration true to omit the XML declaration
    * @return the written document, or null if the conversion failed
    * @throws Exception
    */
    public static String writeDocument(Document doc, boolean omitXmlDeclaration) throws Exception {

        String result = null;
        DOMSource domSource = new DOMSource(doc);
        StringWriter writer = new StringWriter();
        StreamResult streamResult = new StreamResult(writer);

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.STANDALONE, "yes");
        if (omitXmlDeclaration)
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");

        transformer.transform(domSource, streamResult);
        result = writer.toString();
        return result;
    }
}

Related

  1. write(Document doc, Result result)
  2. write(String filename, Document document, boolean addDocType)
  3. write2xml(Document document)
  4. write2Xml(Document document)
  5. write_DOM_into_an_HTML_file(Document doc, String htmlFile, String xslFile)
  6. writeDocument(Document document)
  7. writeDocumentToStreamResult(Document document, StreamResult outputTarget)
  8. writeToBytes(Document document)
  9. writeXML(Document d)