Java XML Document Format prettyPrint(final Document aNode)

Here you can find the source of prettyPrint(final Document aNode)

Description

pretty Print

License

Open Source License

Declaration

public static final String prettyPrint(final Document aNode) throws Exception 

Method Source Code

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

import java.io.StringWriter;

import org.w3c.dom.DOMConfiguration;
import org.w3c.dom.Document;

import org.w3c.dom.bootstrap.DOMImplementationRegistry;
import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSOutput;
import org.w3c.dom.ls.LSSerializer;

public class Main {
    public static final String prettyPrint(final Document aNode) throws Exception {
        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();

        DOMImplementationLS impls = (DOMImplementationLS) registry.getDOMImplementation("LS");

        // Prepare the output
        LSOutput domOutput = impls.createLSOutput();
        domOutput.setEncoding(java.nio.charset.Charset.defaultCharset().name());
        StringWriter writer = new StringWriter();
        domOutput.setCharacterStream(writer);
        LSSerializer domWriter = impls.createLSSerializer();
        DOMConfiguration domConfig = domWriter.getDomConfig();
        domConfig.setParameter("format-pretty-print", true);
        domConfig.setParameter("element-content-whitespace", true);
        domWriter.setNewLine("\r\n");
        domConfig.setParameter("cdata-sections", Boolean.TRUE);
        // And finaly, write
        domWriter.write(aNode, domOutput);
        return domOutput.getCharacterStream().toString();
    }//from  ww w .j  a  v  a 2  s. c o m
}

Related

  1. formatXML(Document xml, Document xsl, URIResolver resolver, Writer output)
  2. pretty(Document document, OutputStream outputStream, int indent)
  3. prettyFormat(Document doc)
  4. prettyPrint(Document doc)
  5. prettyPrint(Document doc, String programID, String xmlTraDestinationFolderPath)
  6. prettyPrintXml(Document doc)
  7. prettyPrintXml(Document document)
  8. prettyPrintXMLDocument(Node node)
  9. print(Document d)