Java XML Document Format pretty(Document document, OutputStream outputStream, int indent)

Here you can find the source of pretty(Document document, OutputStream outputStream, int indent)

Description

pretty

License

Apache License

Declaration

public static void pretty(Document document, OutputStream outputStream,
            int indent) throws IOException 

Method Source Code

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

import org.w3c.dom.Document;

import javax.xml.transform.*;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import java.io.*;

public class Main {
    public static void pretty(Document document, OutputStream outputStream,
            int indent) throws IOException {
        TransformerFactory transformerFactory = TransformerFactory
                .newInstance();/*  w ww .ja  v  a 2  s . c o  m*/
        Transformer transformer = null;
        try {
            transformer = transformerFactory.newTransformer();
        } catch (TransformerConfigurationException e) {
            throw new RuntimeException(e);
        }
        transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        if (indent > 0) {
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty(
                    "{http://xml.apache.org/xslt}indent-amount",
                    Integer.toString(indent));
        }
        Result result = new StreamResult(outputStream);
        Source source = new DOMSource(document);
        try {
            transformer.transform(source, result);
        } catch (TransformerException e) {
            throw new IOException(e);
        }
    }
}

Related

  1. formatNode(Document doc, Node parent, Node node)
  2. formatPython(int start, Document doc, String s)
  3. formatXML(Document document, org.w3c.dom.Element root, String tab)
  4. formatXML(Document xml, Document xsl)
  5. formatXML(Document xml, Document xsl, URIResolver resolver, Writer output)
  6. prettyFormat(Document doc)
  7. prettyPrint(Document doc)
  8. prettyPrint(Document doc, String programID, String xmlTraDestinationFolderPath)
  9. prettyPrint(final Document aNode)