Java XML Node to String xmlToBytes(final Node body)

Here you can find the source of xmlToBytes(final Node body)

Description

xml To Bytes

License

Open Source License

Declaration

public static byte[] xmlToBytes(final Node body) throws TransformerException 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.io.ByteArrayOutputStream;
import javax.xml.transform.Result;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
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.Node;

public class Main {
    private static final TransformerFactory FACTORY = TransformerFactory.newInstance();

    public static byte[] xmlToBytes(final Node body) throws TransformerException {
        final Transformer transformer = FACTORY.newTransformer();
        final Source source = new DOMSource(body);
        final ByteArrayOutputStream buffer = new ByteArrayOutputStream();
        final Result result = new StreamResult(buffer);
        transformer.transform(source, result);
        return buffer.toByteArray();
    }/*  w w w .  j a va2s  . c om*/
}

Related

  1. toXMLString(Node node)
  2. toXMLString(Node node, boolean header)
  3. writeXml(Node n, OutputStream os)
  4. xml(Node node)
  5. XML2String(Node node)
  6. XmlToSource(Node node)
  7. xmlToStream(Node n, OutputStream os)
  8. xmlToStreamE(Node n, OutputStream os)
  9. xmlToString(Node doc)