print Xml via Transformation - Java XML

Java examples for XML:XML Transform

Description

print Xml via Transformation

Demo Code


//package com.java2s;

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

import java.io.UnsupportedEncodingException;
import java.io.Writer;

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;

public class Main {
    public static void printXml(Document xml, Writer out)
            throws TransformerException, UnsupportedEncodingException {
        Transformer transformer = TransformerFactory.newInstance()
                .newTransformer();//from  w  ww  .  j  av a 2  s . c o m
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");

        transformer.transform(new DOMSource(xml), new StreamResult(out));
    }

    public static void printXml(Document response, OutputStream out)
            throws UnsupportedEncodingException, TransformerException {
        printXml(response, new OutputStreamWriter(out));
    }
}

Related Tutorials