Java XML Format print(Node node, OutputStream out)

Here you can find the source of print(Node node, OutputStream out)

Description

print

License

Apache License

Declaration

public static void print(Node node, OutputStream out)
            throws UnsupportedEncodingException, TransformerException 

Method Source Code

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

import java.io.IOException;

import java.io.OutputStream;
import java.io.OutputStreamWriter;

import java.io.UnsupportedEncodingException;

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 org.w3c.dom.Document;

import org.w3c.dom.Node;

public class Main {
    private Document doc;

    public static void print(Node node, OutputStream out)
            throws UnsupportedEncodingException, 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");

        transformer.transform(new DOMSource(node), new StreamResult(new OutputStreamWriter(out, "UTF-8")));
    }/*from   w w w . j a  va 2 s  .c  o m*/

    public void print(OutputStream out) throws IOException, TransformerException {
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "no");
        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");

        transformer.transform(new DOMSource(this.doc), new StreamResult(new OutputStreamWriter(out, "UTF-8")));
    }
}

Related

  1. prettyXmlPrint(Node xml, OutputStream out)
  2. print(Element elm, OutputStream out)
  3. print(File file)
  4. print(Node node)
  5. print(Node node)
  6. print(Node node, String encoding)
  7. print(Node node, Writer out, boolean omitdecl)
  8. print(SOAPMessage msg, OutputStream out)
  9. printAttributeValue(Element node, String prefix)