Java XML Document Serialize serializeXmlObject(org.w3c.dom.Document docToSerialize)

Here you can find the source of serializeXmlObject(org.w3c.dom.Document docToSerialize)

Description

Serialize w3c dom document.

License

Open Source License

Parameter

Parameter Description
docToSerialize a parameter

Exception

Parameter Description
Exception an exception

Declaration

public static String serializeXmlObject(org.w3c.dom.Document docToSerialize) throws Exception 

Method Source Code

//package com.java2s;

public class Main {
    /**//from   w w w  .j  av a  2  s.c  o m
     * Serialize w3c dom document.
     * 
     * @param docToSerialize
     * @return
     * @throws Exception
     */
    public static String serializeXmlObject(org.w3c.dom.Document docToSerialize) throws Exception {

        // Get a factory (DOMImplementationLS) for creating a Load and Save object.
        org.w3c.dom.ls.DOMImplementationLS impl = (org.w3c.dom.ls.DOMImplementationLS) org.w3c.dom.bootstrap.DOMImplementationRegistry
                .newInstance().getDOMImplementation("LS");

        // Use the factory to create an object (LSSerializer) used to 
        // write out or save the document.
        org.w3c.dom.ls.LSSerializer writer = impl.createLSSerializer();
        org.w3c.dom.DOMConfiguration config = writer.getDomConfig();
        config.setParameter("xml-declaration", false);
        config.setParameter("format-pretty-print", Boolean.TRUE);

        // Use the LSSerializer to write out or serialize the document to a String.
        String serializedXML = writer.writeToString(docToSerialize);
        return serializedXML;
    }
}

Related

  1. serializeDOM(Document doc)
  2. serializeToByteArray(Document doc)
  3. serializeToString(org.w3c.dom.Document doc)
  4. serializeToStringLS(Document doc, Node node, String encoding)
  5. serializeXML(Document doc, Writer writer, boolean addXmlDeclaration)
  6. serializeXMLToFile(Document document, File output)