Example usage for org.w3c.dom.ls LSSerializer write

List of usage examples for org.w3c.dom.ls LSSerializer write

Introduction

In this page you can find the example usage for org.w3c.dom.ls LSSerializer write.

Prototype

public boolean write(Node nodeArg, LSOutput destination) throws LSException;

Source Link

Document

Serialize the specified node as described above in the general description of the LSSerializer interface.

Usage

From source file:Main.java

/**
 * Writes the given document to a stream (pretty-printed)
 * // w  ww  . j  av  a  2 s .  c  om
 * @param doc Document to serialize
 * @param out the Stream to write to
 * @param encoding The encoding to use
 */
public static void writeDocument(Document doc, OutputStream out, String encoding) {
    if (doc == null)
        return;
    LSSerializer writer = impl.createLSSerializer();
    writer.getDomConfig().setParameter("format-pretty-print", Boolean.TRUE);
    LSOutput lsOutput = impl.createLSOutput();
    lsOutput.setByteStream(out);
    lsOutput.setEncoding(encoding);
    writer.write(doc, lsOutput);
}

From source file:Main.java

/**
 * Serialize XML Node to string/*w w  w . j a  v  a 2  s.  co  m*/
 * <p>
 * Note: this method is supposed to be faster than the Transform version but the output control
 * is limited. If node is Document node, it will output XML PI which sometimes we want to avoid.
 * 
 * @param doc XML document
 * @param node Node to be serialized
 * @param encoding encoding for the output
 * @return String representation of the Document
 * @throws IOException
 */
public static String serializeToStringLS(Document doc, Node node, String encoding) throws IOException {
    DOMImplementationLS domImpl = (DOMImplementationLS) doc.getImplementation();
    LSSerializer lsSerializer = domImpl.createLSSerializer();
    LSOutput output = domImpl.createLSOutput();
    output.setEncoding(encoding);
    StringWriter writer = new StringWriter();
    output.setCharacterStream(writer);
    lsSerializer.write(node, output);
    writer.flush();

    return writer.toString();
}

From source file:Main.java

/**
 * Writes a Node out to an OutputStream using the DOM, level 3, Load/Save serializer. The written content 
 * is encoded using the encoding specified in the output stream configuration.
 * /*from   w  w w.j  a v  a 2  s.com*/
 * @param node the node to write out
 * @param output the output stream to write the XML to
 * @param serializerParams parameters to pass to the {@link DOMConfiguration} of the serializer
 *         instance, obtained via {@link LSSerializer#getDomConfig()}. May be null.
 */
public static void writeNode(Node node, OutputStream output, Map<String, Object> serializerParams) {
    DOMImplementationLS domImplLS = getLSDOMImpl(node);

    LSSerializer serializer = getLSSerializer(domImplLS, serializerParams);

    LSOutput serializerOut = domImplLS.createLSOutput();
    serializerOut.setByteStream(output);

    serializer.write(node, serializerOut);
}

From source file:Main.java

/**
 * Writes a Node out to a Writer using the DOM, level 3, Load/Save serializer. The written content is encoded using
 * the encoding specified in the writer configuration.
 * /*from w w  w. j  a v a2 s.  com*/
 * @param node the node to write out
 * @param output the writer to write the XML to
 * @param serializerParams parameters to pass to the {@link DOMConfiguration} of the serializer
 *         instance, obtained via {@link LSSerializer#getDomConfig()}. May be null.
 */
public static void writeNode(Node node, Writer output, Map<String, Object> serializerParams) {
    DOMImplementationLS domImplLS = getLSDOMImpl(node);

    LSSerializer serializer = getLSSerializer(domImplLS, serializerParams);

    LSOutput serializerOut = domImplLS.createLSOutput();
    serializerOut.setCharacterStream(output);

    serializer.write(node, serializerOut);
}

From source file:Main.java

public static String elementToString(Element element) {
    Document document = element.getOwnerDocument();
    DOMImplementationLS domImplLS = (DOMImplementationLS) document.getImplementation();
    LSSerializer serializer = domImplLS.createLSSerializer();

    // Client assumes/requires UTF-8 response.
    LSOutput lsOutput = domImplLS.createLSOutput();
    lsOutput.setEncoding("UTF-8");

    StringWriter stringWriter = new StringWriter();
    lsOutput.setCharacterStream(stringWriter);
    serializer.write(element, lsOutput);

    return stringWriter.toString();
}

From source file:XMLUtils.java

public static InputStream getInputStream(Document doc) throws Exception {
    DOMImplementationLS impl = null;
    DOMImplementation docImpl = doc.getImplementation();
    // Try to get the DOMImplementation from doc first before
    // defaulting to the sun implementation.
    if (docImpl != null && docImpl.hasFeature("LS", "3.0")) {
        impl = (DOMImplementationLS) docImpl.getFeature("LS", "3.0");
    } else {/*from   ww  w .  j a  v  a 2 s  .  c  om*/
        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
        impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
        if (impl == null) {
            System.setProperty(DOMImplementationRegistry.PROPERTY,
                    "com.sun.org.apache.xerces.internal.dom.DOMImplementationSourceImpl");
            registry = DOMImplementationRegistry.newInstance();
            impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
        }
    }
    LSOutput output = impl.createLSOutput();
    ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
    output.setByteStream(byteArrayOutputStream);
    LSSerializer writer = impl.createLSSerializer();
    writer.write(doc, output);
    byte[] buf = byteArrayOutputStream.toByteArray();
    return new ByteArrayInputStream(buf);
}

From source file:Main.java

public static boolean serialize(Document doc, boolean setXmlDecl, File f) throws FileNotFoundException {
    DOMImplementationLS lsImpl = (DOMImplementationLS) doc.getImplementation().getFeature("LS", "3.0");
    LSSerializer serializer = lsImpl.createLSSerializer();
    serializer.getDomConfig().setParameter("xml-declaration", setXmlDecl); // set it to false to get

    LSOutput output = lsImpl.createLSOutput();
    output.setByteStream(new FileOutputStream(f));
    return serializer.write(doc, output);
}

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);
    return domOutput.getCharacterStream().toString();
}

From source file:Main.java

/**
 * Converts a dom element to a String//from   www. jav  a2 s. c  o m
 * 
 * @param node
 * @return the dom as a String
 */
public static String writeDomToString(Element node) {
    DOMImplementation domImplementation = node.getOwnerDocument().getImplementation();
    if (domImplementation.hasFeature("LS", "3.0") && domImplementation.hasFeature("Core", "2.0")) {
        DOMImplementationLS domImplementationLS = (DOMImplementationLS) domImplementation.getFeature("LS",
                "3.0");
        LSSerializer lsSerializer = domImplementationLS.createLSSerializer();

        LSOutput lsOutput = domImplementationLS.createLSOutput();
        lsOutput.setEncoding("UTF-8");

        StringWriter stringWriter = new StringWriter();
        lsOutput.setCharacterStream(stringWriter);
        lsSerializer.write(node, lsOutput);
        return stringWriter.toString();
    } else {
        throw new RuntimeException("DOM 3.0 LS and/or DOM 2.0 Core not supported.");
    }
}

From source file:Main.java

private static void serializeXML(Element doc, Writer writer, boolean addXmlDeclaration) throws IOException {
    try {// w w w. j av  a 2s.  c  o m
        DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();
        DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
        LSSerializer serializer = impl.createLSSerializer();
        DOMConfiguration config = serializer.getDomConfig();
        config.setParameter("xml-declaration", addXmlDeclaration);
        // config.setParameter("format-pretty-print", true);
        // config.setParameter("normalize-characters", true);
        LSOutput out = impl.createLSOutput();
        out.setCharacterStream(writer);
        serializer.write(doc, out);
    } catch (Throwable e) {
        throw new IOException(e.getMessage());
    }
}