Java XML Document to String xmlDocumentToString(Document document)

Here you can find the source of xmlDocumentToString(Document document)

Description

Write a DOM document to a string

License

Open Source License

Declaration

public static String xmlDocumentToString(Document document) 

Method Source Code

//package com.java2s;
/**/*  w ww  . j  a  v  a 2  s. c  o m*/
 * e-Science Central
 * Copyright (C) 2008-2013 School of Computing Science, Newcastle University
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License
 * version 2 as published by the Free Software Foundation at:
 * http://www.gnu.org/licenses/gpl-2.0.html
 *
 * This program 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, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, 5th Floor, Boston, MA 02110-1301, USA.
 */

import org.w3c.dom.Document;

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

import java.io.*;

public class Main {
    /** Write a DOM document to a string */
    public static String xmlDocumentToString(Document document) {
        try {
            Source source = new DOMSource(document);
            StringWriter stringWriter = new StringWriter();
            Result result = new StreamResult(stringWriter);
            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer = factory.newTransformer();
            transformer.transform(source, result);
            return stringWriter.getBuffer().toString();
        } catch (TransformerConfigurationException e) {
            e.printStackTrace();
        } catch (TransformerException e) {
            e.printStackTrace();
        }
        return null;
    }
}

Related

  1. xmlDocToString(Document doc)
  2. xmlDocumentDecode(final String xmlURI)
  3. xmlDocumentToString(Document doc)
  4. xmlDocumentToString(Document doc)
  5. xmlDocumentToString(Document doc)
  6. xmlDocumentToString(final Document document)
  7. xmlDocumentToString(Node d)
  8. xmlDOMDocumentToString(Document doc)
  9. XMLDOMtoString(Document document)