Example usage for org.w3c.dom.ls DOMImplementationLS createLSOutput

List of usage examples for org.w3c.dom.ls DOMImplementationLS createLSOutput

Introduction

In this page you can find the example usage for org.w3c.dom.ls DOMImplementationLS createLSOutput.

Prototype

public LSOutput createLSOutput();

Source Link

Document

Create a new empty output destination object where LSOutput.characterStream, LSOutput.byteStream, LSOutput.systemId, LSOutput.encoding are null.

Usage

From source file:test.framework.TestBase.java

/**
 * Serialize the given Dom Object to a String.
 * // w  w  w .  java2 s . c o m
 * @param xml The Xml Node to serialize.
 * @param omitXMLDeclaration Indicates if XML declaration will be omitted.
 * @return The String representation of the Xml Node.
 * @throws Exception If anything fails.
 */
protected static String toString(final Node xml, final boolean omitXMLDeclaration) throws Exception {
    String result = new String();
    if (xml instanceof AttrImpl) {
        result = xml.getTextContent();
    } else if (xml instanceof Document) {
        StringWriter stringOut = new StringWriter();
        // format
        OutputFormat format = new OutputFormat((Document) xml);
        format.setIndenting(true);
        format.setPreserveSpace(false);
        format.setOmitXMLDeclaration(omitXMLDeclaration);
        format.setEncoding("UTF-8");
        // serialize
        XMLSerializer serial = new XMLSerializer(stringOut, format);
        serial.asDOMSerializer();
        serial.serialize((Document) xml);
        result = stringOut.toString();
    } else {
        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
        DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
        LSOutput lsOutput = impl.createLSOutput();
        lsOutput.setEncoding("UTF-8");
        ByteArrayOutputStream os = new ByteArrayOutputStream();
        lsOutput.setByteStream(os);
        LSSerializer writer = impl.createLSSerializer();
        // result = writer.writeToString(xml);
        writer.write(xml, lsOutput);
        result = ((ByteArrayOutputStream) lsOutput.getByteStream()).toString();
        if ((omitXMLDeclaration) && (result.indexOf("?>") != -1)) {
            result = result.substring(result.indexOf("?>") + 2);
        }
        // result = toString(getDocument(writer.writeToString(xml)),
        // true);
    }
    return result;
}