Java XML Document to Byte Array documentToByteArray(Document doc)

Here you can find the source of documentToByteArray(Document doc)

Description

converts a Document into an UTF-8 encoded byte array

License

Open Source License

Parameter

Parameter Description
doc the Document to convert

Exception

Parameter Description
TransformerConfigurationException thrown when the Transformer couldn't be configured
TransformerException thrown when the Transformer had a problem

Return

the resulting byte array

Declaration

public static byte[] documentToByteArray(Document doc)
        throws TransformerConfigurationException, TransformerException 

Method Source Code

//package com.java2s;
/*//from w w w  . j  ava 2  s  . c o  m
 *  This file is part of "Tweety", a collection of Java libraries for
 *  logical aspects of artificial intelligence and knowledge representation.
 *
 *  Tweety is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License version 3 as
 *  published by the Free Software Foundation.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import java.nio.charset.Charset;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.Transformer;
import javax.xml.transform.dom.*;
import javax.xml.transform.stream.*;

import java.io.StringWriter;

import org.w3c.dom.Document;

public class Main {
    /**
     * converts a {@code Document} into an UTF-8 encoded {@code byte} array
     * @param doc the {@code Document} to convert
     * @return the resulting {@code byte} array
     * @throws TransformerConfigurationException thrown when the {@code Transformer} couldn't be configured
     * @throws TransformerException thrown when the {@code Transformer} had a problem
     */
    public static byte[] documentToByteArray(Document doc)
            throws TransformerConfigurationException, TransformerException {
        String docString = documentToString(doc);
        byte[] stringContent = docString.getBytes(Charset.forName("UTF-8"));
        return stringContent;
    }

    /**
     * Convert from {@code Document} to {@code String}
     * @param doc the {@code Document} to convert
     * @return the resulting {@code String}
     */
    private static String documentToString(Document doc)
            throws TransformerConfigurationException, TransformerException {
        DOMSource domSource = new DOMSource(doc);
        StringWriter writer = new StringWriter();
        StreamResult result = new StreamResult(writer);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.transform(domSource, result);
        return writer.toString();
    }
}

Related

  1. asByteArray(Document doc)
  2. asByteArray(Document doc, String encoding)
  3. documentToByteArray(Document data, Integer indent)
  4. documentToBytes(Document doc)
  5. dumpToByteArray(Document document)