Java XML Node Serialize serialiseBytes(Node n)

Here you can find the source of serialiseBytes(Node n)

Description

serialise Bytes

License

Open Source License

Declaration

public static byte[] serialiseBytes(Node n) 

Method Source Code

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

import org.w3c.dom.Node;

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 java.io.ByteArrayOutputStream;
import java.io.File;

import java.io.OutputStream;

import java.io.StringWriter;
import java.io.Writer;

public class Main {
    public static byte[] serialiseBytes(Node n) {
        ByteArrayOutputStream bos = new ByteArrayOutputStream(1024);

        serialise(n, bos);//from w w  w . j  a v a 2s.  c o  m

        return bos.toByteArray();
    }

    public static String serialise(Node n) {
        StringWriter writer = new StringWriter(1024);

        serialise(n, writer);

        return writer.toString();
    }

    public static void serialise(Node n, StreamResult result) {
        if (n == null)
            throw new IllegalArgumentException("Must provide non-null XML node to serialise!");
        if (result == null)
            throw new IllegalArgumentException("Must provide non-null output to serialise to!");

        try {
            Transformer transform = TransformerFactory.newInstance().newTransformer();
            transform.transform(new DOMSource(n), result);
        } catch (TransformerConfigurationException e) {
            throw new RuntimeException("Error serialising node: " + e.getMessage(), e);
        } catch (TransformerException e) {
            throw new RuntimeException("Error serialising node: " + e.getMessage(), e);
        }
    }

    public static void serialise(Node n, OutputStream os) {
        if (n == null)
            throw new IllegalArgumentException("Must provide non-null XML node to serialise!");
        if (os == null)
            throw new IllegalArgumentException("Must provide non-null output to serialise to!");

        serialise(n, new StreamResult(os));
    }

    public static void serialise(Node n, Writer writer) {
        if (n == null)
            throw new IllegalArgumentException("Must provide non-null XML node to serialise!");
        if (writer == null)
            throw new IllegalArgumentException("Must provide non-null output to serialise to!");

        serialise(n, new StreamResult(writer));
    }

    public static void serialise(Node n, File file) {
        if (n == null)
            throw new IllegalArgumentException("Must provide non-null XML node to serialise!");
        if (file == null)
            throw new IllegalArgumentException("Must provide non-null File to serialise to!");

        serialise(n, new StreamResult(file));
    }
}

Related

  1. getSerializer(final File file)
  2. serialise(Node node)
  3. serialize(Element e, boolean omitXMLDeclaration)
  4. serialize(Node doc)
  5. serialize(Node n, StreamResult sr)
  6. serialize(Node node)