Java XML Format formattedPrint(Node xml, OutputStream out)

Here you can find the source of formattedPrint(Node xml, OutputStream out)

Description

formatted Print

License

Open Source License

Declaration

public static void formattedPrint(Node xml, OutputStream out) throws TransformerConfigurationException,
            TransformerFactoryConfigurationError, TransformerException, UnsupportedEncodingException 

Method Source Code

//package com.java2s;

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.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Node;

public class Main {
    public static void formattedPrint(Node xml, OutputStream out) throws TransformerConfigurationException,
            TransformerFactoryConfigurationError, TransformerException, UnsupportedEncodingException {
        TransformerFactory tFactory = TransformerFactory.newInstance();
        // tFactory.setAttribute("indent-number", 4);
        Transformer tf = tFactory.newTransformer();
        tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        tf.setOutputProperty(OutputKeys.INDENT, "yes");
        tf.setOutputProperty(OutputKeys.METHOD, "xml");
        tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "5");
        StreamResult result = new StreamResult(new OutputStreamWriter(out, "UTF-8"));
        tf.transform(new DOMSource(xml), result);
    }// w ww  . ja v a 2 s . c o m
}

Related

  1. format(final String xml)
  2. format(Node node)
  3. format(Node node, String indent)
  4. format(String unformattedXml)
  5. formatAttributes(Node node)
  6. formatXML(byte[] xmlData, int indent)
  7. formatXML(String unformatted)
  8. formatXml(String xml)
  9. formatXML(String xml)