Java XML Document to String documentToString(Document doc)

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

Description

Converts a Document object into String representation

License

Open Source License

Parameter

Parameter Description
doc the Document to transform

Exception

Parameter Description
TransformerException an exception

Return

a String containing the String representation of the document

Declaration

public static String documentToString(Document doc) throws TransformerException 

Method Source Code

//package com.java2s;
/**//w w w .j  ava  2  s.c  o m
 * (C) Copyright 2015 Zaizi Limited (http://www.zaizi.com).
 *
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Lesser General Public License
 * (LGPL) version 3.0 which accompanies this distribution, and is available at 
 * http://www.gnu.org/licenses/lgpl-3.0.en.html
 *
 * 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.
 *
 **/

import org.w3c.dom.Document;
import org.w3c.dom.Element;

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 {
    /**
     * <p>Converts a {@code Document} object into {@code String} representation</p>
     * @param doc the {@code Document} to transform
     * @return a {@code String} containing the String representation of the document
     * @throws TransformerException
     */
    public static String documentToString(Document doc) throws TransformerException {
        Element element = doc.getDocumentElement();
        return elementToString(element);
    }

    /**
     * <p>Converts an {@code Element} object into {@code String} representation</p>
     * @param element
     * @return
     * @throws TransformerException
     */
    public static String elementToString(Element element) throws TransformerException {
        DOMSource domSource = new DOMSource(element);
        StringWriter stringWriter = new StringWriter();
        StreamResult streamResult = new StreamResult(stringWriter);

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

        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.transform(domSource, streamResult);

        return stringWriter.toString();
    }
}

Related

  1. documentToString(Document d)
  2. documentToString(Document doc)
  3. documentToString(Document doc)
  4. documentToString(Document doc)
  5. documentToString(Document doc)
  6. documentToString(Document doc)
  7. documentToString(Document doc)
  8. documentToString(Document doc)
  9. documentToString(Document doc)