Java XML Document to Byte Array dumpToByteArray(Document document)

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

Description

dump To Byte Array

License

Open Source License

Declaration

public static byte[] dumpToByteArray(Document document)
            throws TransformerConfigurationException, TransformerException 

Method Source Code


//package com.java2s;
/* Copyright (c) 2013 Intecs - www.intecs.it. All rights reserved.
 * This code is licensed under the GPL 3.0 license, available at the root
 * application directory./* w  w w .ja v a 2s .c  om*/
*/

import java.io.*;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Document;

public class Main {
    public static byte[] dumpToByteArray(Document document)
            throws TransformerConfigurationException, TransformerException {
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();

        ByteArrayOutputStream bos = new ByteArrayOutputStream();
        StreamResult result = new StreamResult(bos);
        transformer.transform(new DOMSource(document), result);
        byte[] array = bos.toByteArray();

        return array;
    }
}

Related

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