Java XML Document to String XMLDOMtoString(Document document)

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

Description

XMLDO Mto String

License

Open Source License

Declaration

public static String XMLDOMtoString(Document document) 

Method Source Code

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

import java.io.StringWriter;

import javax.xml.transform.Result;
import javax.xml.transform.Source;
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 XMLDOMtoString(Document document) {

        String out = null;/*from   w  w w. j a  v a 2  s .  c om*/
        try {
            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer = factory.newTransformer();
            StringWriter writer = new StringWriter();
            Result result = new StreamResult(writer);
            Source source = new DOMSource(document);
            transformer.transform(source, result);
            writer.close();
            out = writer.toString();
        } catch (Exception e) {
            System.out.println("ERROR: " + e.getMessage());

        }

        return out;
    }
}

Related

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