Java XML Document to String getDocumentAsString(Document doc)

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

Description

get Document As String

License

Open Source License

Declaration

public static String getDocumentAsString(Document doc) throws TransformerException 

Method Source Code

//package com.java2s;
/**/*ww w.ja  v a  2 s . c o m*/
 * Copyright (C) 2013 Inera AB (http://www.inera.se)
 *
 * This file is part of Inera MessageService (http://code.google.com/p/inera-message).
 *
 * Inera MessageService 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.
 *
 * Inera MessageService 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.StringWriter;

import javax.xml.transform.*;

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

import org.w3c.dom.Document;

public class Main {
    private static TransformerFactory tf;

    public static String getDocumentAsString(Document doc) throws TransformerException {
        StringWriter sw = new StringWriter();
        tf.newTransformer().transform(new DOMSource(doc), new StreamResult(sw));
        return sw.toString();
    }
}

Related

  1. documentToString(Node document)
  2. dom2String2(Document document)
  3. DOMaXML(Document doc, String nome)
  4. domToString(Document document)
  5. domToString(final Document document)
  6. getDocumentAsString(Document doc)
  7. getDocumentAsString(Document document)
  8. getDocumentAsString(Node node, boolean prettyXml)
  9. getDocumentAsString(Resource resource)