Java XML Document to String toString(Document document)

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

Description

convert org.w3c.dom.Document to xml-String

License

Open Source License

Parameter

Parameter Description
document a parameter

Exception

Parameter Description
Exception an exception

Return

String xml

Declaration

private static String toString(Document document) throws Exception 

Method Source Code

//package com.java2s;
/*/*from ww  w  . j a  v  a  2s  .c  om*/
 * CDDL HEADER START
 *
 * The contents of this file are subject to the terms of the
 * Common Development and Distribution License, Version 1.0 only
 * (the "License").  You may not use this file except in compliance
 * with the License.
 *
 * You can obtain a copy of the license at license/ESCIDOC.LICENSE
 * or http://www.escidoc.de/license.
 * See the License for the specific language governing permissions
 * and limitations under the License.
 *
 * When distributing Covered Code, include this CDDL HEADER in each
 * file and include the License file at license/ESCIDOC.LICENSE.
 * If applicable, add the following below this CDDL HEADER, with the
 * fields enclosed by brackets "[]" replaced with your own identifying
 * information: Portions Copyright [yyyy] [name of copyright owner]
 *
 * CDDL HEADER END
 */

import java.io.StringWriter;

import javax.xml.transform.Result;
import javax.xml.transform.Source;
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 {
    /**
     * convert org.w3c.dom.Document to xml-String
     * 
     * @param document
     * @return String xml
     * @throws Exception
     */
    private static String toString(Document document) throws Exception {
        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();
    }
}

Related

  1. toString(Document doc, String encoding, boolean indent)
  2. toString(Document document)
  3. toString(Document document)
  4. toString(Document document)
  5. toString(Document document)
  6. toString(final Document document)
  7. toString(final Document document)
  8. toString(final Document document, final boolean indent, final boolean omitXmlDeclaration)
  9. toString(Node document)