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

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

Introduction

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

Prototype

public LSSerializer createLSSerializer();

Source Link

Document

Create a new LSSerializer object.

Usage

From source file:org.zaproxy.zap.extension.exportreport.Export.ReportExport.java

public static String getStringFromDoc(Document doc) {
    DOMImplementationLS domImplementation = (DOMImplementationLS) doc.getImplementation();
    LSSerializer lsSerializer = domImplementation.createLSSerializer();
    return lsSerializer.writeToString(doc);
}

From source file:test.framework.TestBase.java

/**
 * Serialize the given Dom Object to a String.
 * //  w  w  w . j  a  v  a  2s.co  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;
}