Java XML Document Print printXmlDocument(Document doc)

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

Description

print Xml Document

License

Open Source License

Declaration

public static void printXmlDocument(Document doc) 

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.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 {
    public static void printXmlDocument(Document doc) {
        System.out.println(xmlDocumentToString(doc));
    }// w ww  . j ava  2 s  .  c o  m

    public static String xmlDocumentToString(Document doc) {
        try {
            DOMSource domSource = new DOMSource(doc);
            StringWriter writer = new StringWriter();
            StreamResult result = new StreamResult(writer);
            TransformerFactory tf = TransformerFactory.newInstance();
            Transformer transformer = tf.newTransformer();
            transformer.transform(domSource, result);
            return writer.toString();
        } catch (TransformerException ex) {
            ex.printStackTrace();
            return null;
        }
    }
}

Related

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