Java XML Document Format printDocument(Document document)

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

Description

print Document

License

Open Source License

Declaration

public static String printDocument(Document document) throws Exception 

Method Source Code

//package com.java2s;
/*/*from w  ww. jav a2  s . c  o  m*/
 * JBoss, Home of Professional Open Source.
 *
 * See the LEGAL.txt file distributed with this work for information regarding copyright ownership and licensing.
 *
 * See the AUTHORS.txt file distributed with this work for a full listing of individual contributors.
 */

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 printDocument(Document document) throws Exception {
        StringWriter writer = new StringWriter();

        try {
            TransformerFactory transformerFactory = TransformerFactory
                    .newInstance();
            Transformer transformer = transformerFactory.newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes"); //$NON-NLS-1$
            transformer.transform(new DOMSource(document),
                    new StreamResult(writer));
            return writer.toString();
        } finally {
            writer.close();
        }
    }
}

Related

  1. printDocument(Document doc, boolean prettyPrint)
  2. printDocument(Document doc, OutputStream out)
  3. printDocument(Document doc, Writer ps)
  4. printDocument(Document document)
  5. printDocument(Document document)
  6. printPrettyXML(Document doc)
  7. saveDocumentToFormattedStream(Document doc, OutputStream outputStream)
  8. toFormattedString(Document element)