Java XML Document to Stream writeXML(Document d, OutputStream out)

Here you can find the source of writeXML(Document d, OutputStream out)

Description

Write the XML-encoding of a Document Document to an java.io.OutputStream OutputStream .

License

Apache License

Parameter

Parameter Description
d Document
out Stream

Exception

Parameter Description
IOException If anything unexpected happens...

Declaration

public static void writeXML(Document d, OutputStream out) throws IOException 

Method Source Code


//package com.java2s;
/*//from   w ww .j  a v a  2s .  c o  m
 *  Copyright 2006-2015 WebPKI.org (http://webpki.org).
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */

import java.io.IOException;
import java.io.ByteArrayOutputStream;
import java.io.OutputStream;

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

public class Main {
    /**
     * Write the XML-encoding of a {@link Document Document} to an 
     * {@link java.io.OutputStream OutputStream}.
     * @param d Document
     * @param out Stream
     * @throws IOException If anything unexpected happens...
     */
    public static void writeXML(Document d, OutputStream out) throws IOException {
        DOMImplementationLS DOMiLS = (DOMImplementationLS) d.getImplementation();
        ;
        LSOutput LSO = DOMiLS.createLSOutput();
        LSO.setByteStream(out);
        LSSerializer LSS = DOMiLS.createLSSerializer();
        if (!LSS.write(d, LSO)) {
            throw new IOException("[Serialization failed!]");
        }
    }

    public static void writeXML(Element e, OutputStream out) throws IOException {
        DOMImplementationLS DOMiLS = (DOMImplementationLS) e.getOwnerDocument().getImplementation();
        ;
        LSOutput LSO = DOMiLS.createLSOutput();
        LSO.setByteStream(out);
        LSSerializer LSS = DOMiLS.createLSSerializer();
        if (!LSS.write(e, LSO)) {
            throw new IOException("[Serialization failed!]");
        }
    }

    public static byte[] writeXML(Document d) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        writeXML(d, baos);
        baos.close();
        return baos.toByteArray();
    }

    public static byte[] writeXML(Element e) throws IOException {
        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        writeXML(e, baos);
        baos.close();
        return baos.toByteArray();
    }
}

Related

  1. writeOutDocument(OutputStream os, Document doc)
  2. writeTransformedXml(Document doc, OutputStream output, InputStream style)
  3. writeXerces(Document doc, OutputStream out, String encoding)
  4. writeXHTML(Document htmldoc, OutputStream out)
  5. writeXML(Document d, OutputStream os, String sysID)
  6. writeXML(Document doc, OutputStream os)
  7. writeXML(Document doc, OutputStream os)
  8. writeXML(Document doc, OutputStream out)
  9. writeXml(Document doc, OutputStream outputStream)