Java XML Document Print printDocument(Node doc)

Here you can find the source of printDocument(Node doc)

Description

print Document

License

Apache License

Declaration

public static String printDocument(Node doc) throws TransformerException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import org.w3c.dom.Node;

import javax.xml.transform.OutputKeys;
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 java.io.StringWriter;

public class Main {
    public static String printDocument(Node doc) throws TransformerException {
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        StringWriter sw = new StringWriter();
        transformer.transform(new DOMSource(doc), new StreamResult(sw));
        return sw.toString();
    }//from ww  w  .  j  a v  a  2  s . c  o m
}

Related

  1. printDocument(File file)
  2. printDocument(final Document doc)
  3. printDocument(Node _docOrNode, OutputStream _outStream)
  4. printDocumentToStdout(final Document document)
  5. printDocumentTree(Node el)
  6. printDOM(File file, Document doc, String encoding)
  7. printElements(Document doc)