Java XML Document to String dom2String2(Document document)

Here you can find the source of dom2String2(Document document)

Description

dom String

License

Apache License

Declaration

public static String dom2String2(Document document) 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.StringWriter;

import org.w3c.dom.Document;

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

public class Main {
    public static String dom2String2(Document document) {
        DOMImplementationRegistry registry;
        try {//from ww  w.j a  va2 s. co  m
            registry = DOMImplementationRegistry.newInstance();
            DOMImplementationLS domImplLS = (DOMImplementationLS) registry.getDOMImplementation("LS");

            LSSerializer ser = domImplLS.createLSSerializer(); // Create a serializer for the DOM
            LSOutput out = domImplLS.createLSOutput();
            StringWriter stringOut = new StringWriter(); // Writer will be a String
            out.setCharacterStream(stringOut);
            ser.write(document, out); // Serialize the DOM

            System.out.println("STRXML = " + stringOut.toString()); // Spit out the DOM as a String

            return stringOut.toString();
        } catch (Exception e) {
            System.err.println("Cannot create registry: " + e.getMessage());
        }
        return null;
    }
}

Related

  1. documentToString(Document document, boolean standalone)
  2. documentToString(Document document, Transformer documentTransformer)
  3. DocumentToString(Document dom)
  4. documentToString(final Node node)
  5. documentToString(Node document)
  6. DOMaXML(Document doc, String nome)
  7. domToString(Document document)
  8. domToString(final Document document)
  9. getDocumentAsString(Document doc)