Example usage for org.w3c.dom.ls LSOutput getCharacterStream

List of usage examples for org.w3c.dom.ls LSOutput getCharacterStream

Introduction

In this page you can find the example usage for org.w3c.dom.ls LSOutput getCharacterStream.

Prototype

public java.io.Writer getCharacterStream();

Source Link

Document

An attribute of a language and binding dependent type that represents a writable stream to which 16-bit units can be output.

Usage

From source file:Main.java

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);//  ww w .  java 2s  .c  o  m
    return domOutput.getCharacterStream().toString();
}