Java XML Document to String XMLtoString(Document doc)

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

Description

XM Lto String

License

Open Source License

Declaration

public static String XMLtoString(Document doc) 

Method Source Code

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

import java.io.StringWriter;

import javax.xml.transform.OutputKeys;
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 {
    public static String XMLtoString(Document doc) {
        String output = null;/* w w w.j av a  2s.com*/
        try {
            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer transformer = tf.newTransformer();
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            StringWriter writer = new StringWriter();
            transformer.transform(new DOMSource(doc), new StreamResult(writer));
            output = writer.getBuffer().toString().replaceAll("\n|\r", "");
        } catch (Exception e) {
            e.printStackTrace();
        }
        return output;
    }
}

Related

  1. xmlDocumentToString(Node d)
  2. xmlDOMDocumentToString(Document doc)
  3. XMLDOMtoString(Document document)
  4. xmlToFile(Document doc, String fileNameToWrite)
  5. xmlToString(Document doc)
  6. xmlToString(Document doc)
  7. xmlToString(Document document)
  8. xmlToString(Document xml)
  9. xmlToString(Document xml)