Java XML Document Serialize serializeToStringLS(Document doc, Node node, String encoding)

Here you can find the source of serializeToStringLS(Document doc, Node node, String encoding)

Description

Serialize XML Node to string

Note: this method is supposed to be faster than the Transform version but the output control is limited.

License

Open Source License

Parameter

Parameter Description
doc XML document
node Node to be serialized
encoding encoding for the output

Exception

Parameter Description
IOException an exception

Return

String representation of the Document

Declaration

public static String serializeToStringLS(Document doc, Node node, String encoding) throws IOException 

Method Source Code

//package com.java2s;
/**/*from  www.j a v a2s . co m*/
 * Copyright (c) 2014-2015 by Wen Yu.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 * 
 * Any modifications to this file must keep this entire header intact.
 * 
 * Change History - most recent changes go on top of previous changes
 *
 * StringUtils.java
 *
 * Who   Date       Description
 * ====  =========  =====================================================
 * WY    09Apr2015  Added null check to findAttribute()
 * WY    03Mar2015  Added serializeToString() and serializeToByteArray()
 * WY    27Feb2015  Added findAttribute() and removeAttribute()
 * WY    23Jan2015  Initial creation - moved XML related methods to here
 */

import java.io.IOException;

import java.io.StringWriter;

import org.w3c.dom.Document;

import org.w3c.dom.Node;

import org.w3c.dom.ls.DOMImplementationLS;
import org.w3c.dom.ls.LSOutput;
import org.w3c.dom.ls.LSSerializer;

public class Main {
    /**
     * Serialize XML Document to string using DOM Level 3 Load/Save
     * 
     * @param doc XML Document
     * @return String representation of the Document
     * @throws IOException
     */
    public static String serializeToStringLS(Document doc) throws IOException {
        return serializeToStringLS(doc, doc);
    }

    /**
     * Serialize XML Document to string using DOM Level 3 Load/Save
     * 
     * @param doc XML Document
     * @param node the Node to serialize
     * @return String representation of the Document
     * @throws IOException
     */
    public static String serializeToStringLS(Document doc, Node node) throws IOException {
        String encoding = doc.getInputEncoding();
        if (encoding == null)
            encoding = "UTF-8";

        return serializeToStringLS(doc, node, encoding);
    }

    /**
     * Serialize XML Node to string
     * <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();
    }
}

Related

  1. serializeDocument(Document document, OutputStream os)
  2. serializeDocument(Document root)
  3. serializeDOM(Document doc)
  4. serializeToByteArray(Document doc)
  5. serializeToString(org.w3c.dom.Document doc)
  6. serializeXML(Document doc, Writer writer, boolean addXmlDeclaration)
  7. serializeXmlObject(org.w3c.dom.Document docToSerialize)
  8. serializeXMLToFile(Document document, File output)