Java XML Document to String writeXmlToString(Document xmlDoc)

Here you can find the source of writeXmlToString(Document xmlDoc)

Description

writeXmlToString - write the given XML document to a string.

License

Apache License

Parameter

Parameter Description
xmlDoc - the document to write.

Exception

Parameter Description
TransformerException an exception

Return

- the string representation of the XML document.

Declaration

public static String writeXmlToString(Document xmlDoc) throws TransformerException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.StringWriter;

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 org.w3c.dom.Document;

public class Main {
    /**/*from  w w w .j  a v  a  2s . c  o m*/
     * writeXmlToString - write the given XML document to a string.
     * 
     * @param xmlDoc    - the document to write.
     * @return          - the string representation of the XML document.
     * @throws TransformerException
     */
    public static String writeXmlToString(Document xmlDoc) throws TransformerException {
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty("omit-xml-declaration", "yes");
        StringWriter writer = new StringWriter();
        transformer.transform(new DOMSource(xmlDoc), new StreamResult(writer));
        String output = writer.getBuffer().toString();
        return output;
    }
}

Related

  1. writeDocument(Document doc, boolean omitXmlDeclaration)
  2. writeDocument(Document document)
  3. writeDocumentToStreamResult(Document document, StreamResult outputTarget)
  4. writeToBytes(Document document)
  5. writeXML(Document d)
  6. xmlDocToString(Document doc)
  7. xmlDocToString(Document doc)
  8. xmlDocumentDecode(final String xmlURI)
  9. xmlDocumentToString(Document doc)