Java XML Document to String getString(Document document)

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

Description

get String

License

LGPL

Declaration

public static String getString(Document document) 

Method Source Code

//package com.java2s;
/*//from w w  w  .  j a va 2  s .  com
 * Copyright (c) 2007-2012 The Broad Institute, Inc.
 * SOFTWARE COPYRIGHT NOTICE
 * This software and its documentation are the copyright of the Broad Institute, Inc. All rights are reserved.
 *
 * This software is supplied without any warranty or guaranteed support whatsoever. The Broad Institute is not responsible for its use, misuse, or functionality.
 *
 * This software is licensed under the terms of the GNU Lesser General Public License (LGPL),
 * Version 2.1 which is available at http://www.opensource.org/licenses/lgpl-2.1.php.
 */

import org.w3c.dom.Document;

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import java.io.StringWriter;

public class Main {
    public static String getString(Document document) {
        StreamResult streamResult;
        try {

            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer = factory.newTransformer();

            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "4");

            streamResult = new StreamResult(new StringWriter());
            DOMSource source = new DOMSource(document);
            transformer.transform(source, streamResult);
        } catch (TransformerException e) {
            return null;
        }

        return streamResult.getWriter().toString();
    }
}

Related

  1. getDocumentAsString(Document doc)
  2. getDocumentAsString(Document doc)
  3. getDocumentAsString(Document document)
  4. getDocumentAsString(Node node, boolean prettyXml)
  5. getDocumentAsString(Resource resource)
  6. getStringFromDoc(org.w3c.dom.Document doc)
  7. getStringFromDocument(Document doc)
  8. getStringFromDocument(Document doc)
  9. getStringFromDOM(Document doc)