Java XML Document Print printXMLDocument(Document document)

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

Description

print XML Document

License

Open Source License

Parameter

Parameter Description
XML Document

Return

string containing the given XML Document contents.

Declaration

public static String printXMLDocument(Document document) 

Method Source Code

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

import java.io.StringWriter;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
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 {
    /** /* www.j av  a 2 s  . c o m*/
    * @param XML Document
    * @return string containing the given XML Document contents.
    */
    public static String printXMLDocument(Document document) {

        String XMLString = new String();
        StringWriter stringWriter = new StringWriter();
        DOMSource source = new DOMSource(document);
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer;

        try {

            transformer = transformerFactory.newTransformer();
            StreamResult result = new StreamResult(stringWriter);
            transformer.transform(source, result);
            StringBuffer stringBuffer = stringWriter.getBuffer();
            XMLString = stringBuffer.toString();

        } catch (TransformerConfigurationException e) {
            e.printStackTrace();
        } catch (TransformerException e) {
            e.printStackTrace();
        }

        return XMLString;
    }
}

Related

  1. printElements(Document dom)
  2. printPI(Document doc, PrintStream pstrm)
  3. printXML(Document doc)
  4. printXml(Document xml, Writer out)
  5. printXmlDocument(Document doc)
  6. printXMLFormat(Document document)