Java XML Document to String documentToString(Document document)

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

Description

document To String

License

Open Source License

Declaration

public static String documentToString(Document document) 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

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 {
    public static String documentToString(Document document) {
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer;/*  w  w w  .j  ava  2  s  . c o m*/
        try {
            transformer = tf.newTransformer();
        } catch (TransformerConfigurationException e) {
            throw new RuntimeException(e);
        }
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        StringWriter writer = new StringWriter();
        try {
            transformer.transform(new DOMSource(document), new StreamResult(writer));
        } catch (TransformerException e) {
            throw new RuntimeException(e);
        }
        return writer.getBuffer().toString().replaceAll("\n|\r", "");
    }
}

Related

  1. documentToString(Document doc)
  2. documentToString(Document document)
  3. documentToString(Document document)
  4. documentToString(Document document)
  5. documentToString(Document document)
  6. documentToString(Document document)
  7. documentToString(Document document)
  8. documentToString(Document document, boolean pretty)
  9. documentToString(Document document, boolean standalone)