Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

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();
    }
}