Java XML Document to String writeDocument(Document document)

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

Description

write Document

License

Open Source License

Declaration

public static void writeDocument(Document document) 

Method Source Code


//package com.java2s;
import org.w3c.dom.bootstrap.DOMImplementationRegistry;
import org.w3c.dom.*;
import org.w3c.dom.ls.DOMImplementationLS;

import org.w3c.dom.ls.LSSerializer;

public class Main {
    public static void writeDocument(Document document) {
        try {/*from  w  ww .ja v a 2  s. co m*/
            DOMImplementationRegistry registry = DOMImplementationRegistry.newInstance();

            DOMImplementationLS impl = (DOMImplementationLS) registry.getDOMImplementation("LS");
            LSSerializer writer = impl.createLSSerializer();
            String documentStr = writer.writeToString(document);
            System.out.println("Serialized document: \n" + documentStr);
        } catch (Exception e) {
            System.out.println("ERROR writing document: \n ");
            e.printStackTrace();
        }
    }
}

Related

  1. write(String filename, Document document, boolean addDocType)
  2. write2xml(Document document)
  3. write2Xml(Document document)
  4. write_DOM_into_an_HTML_file(Document doc, String htmlFile, String xslFile)
  5. writeDocument(Document doc, boolean omitXmlDeclaration)
  6. writeDocumentToStreamResult(Document document, StreamResult outputTarget)
  7. writeToBytes(Document document)
  8. writeXML(Document d)
  9. writeXmlToString(Document xmlDoc)