Java XML Document to Byte Array documentToBytes(Document doc)

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

Description

document To Bytes

License

Apache License

Declaration

public static byte[] documentToBytes(Document doc) throws Exception 

Method Source Code

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

import java.io.ByteArrayOutputStream;

import javax.xml.transform.Transformer;
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[] documentToBytes(Document doc) throws Exception {
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        DOMSource source = new DOMSource(doc);
        ByteArrayOutputStream output = new ByteArrayOutputStream();
        StreamResult result = new StreamResult(output);
        transformer.transform(source, result);
        byte[] bytes = output.toByteArray();
        return bytes;
    }/*from   ww  w  .  j  a  va  2s  .co m*/
}

Related

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