Java XML Format print(Element elm, OutputStream out)

Here you can find the source of print(Element elm, OutputStream out)

Description

Print part of document

License

Open Source License

Parameter

Parameter Description
elm Root element which should be written
out OuputStream

Exception

Parameter Description
TransformerException an exception

Declaration

public static void print(Element elm, OutputStream out) throws TransformerException 

Method Source Code

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

import java.io.OutputStream;

import java.io.Writer;

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.Element;

public class Main {
    /**/* ww  w .  j  a v  a2 s.co  m*/
     * Serialize W3C document into given output stream
     * @param doc W3C document
     * @param out OutputStream 
     * @throws TransformerException
     */
    public static void print(Document doc, OutputStream out) throws TransformerException {
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer();

        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(out);
        transformer.transform(source, result);
    }

    /**
     * Print document to given writer
     * @param doc Printing document
     * @param out Writer
     * @throws TransformerException
     */
    public static void print(Document doc, Writer out) throws TransformerException {
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer();

        DOMSource source = new DOMSource(doc);
        StreamResult result = new StreamResult(out);
        transformer.transform(source, result);
    }

    /**
     * Print part of document 
     * @param elm Root element which should be written
     * @param out OuputStream
     * @throws TransformerException
     */
    public static void print(Element elm, OutputStream out) throws TransformerException {
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer();

        DOMSource source = new DOMSource(elm);
        StreamResult result = new StreamResult(out);
        transformer.transform(source, result);
    }

    /**
     * Print part of document 
     * @param elm Root element which should be written
     * @param out Writer
     * @throws TransformerException
     */
    public static void print(Element elm, Writer out) throws TransformerException {
        TransformerFactory tFactory = TransformerFactory.newInstance();
        Transformer transformer = tFactory.newTransformer();

        DOMSource source = new DOMSource(elm);
        StreamResult result = new StreamResult(out);
        transformer.transform(source, result);
    }
}

Related

  1. prettyXml(final String xml)
  2. prettyXml(String xml)
  3. prettyXml(String xml)
  4. prettyXml(String xml)
  5. prettyXmlPrint(Node xml, OutputStream out)
  6. print(File file)
  7. print(Node node)
  8. print(Node node)
  9. print(Node node, OutputStream out)