Java XML Node Serialize serializeNode(Node node)

Here you can find the source of serializeNode(Node node)

Description

Serialize the given DOM node.

License

EUPL

Parameter

Parameter Description
node The node to serialize.

Exception

Parameter Description
TransformerException An error occurred transforming thenode to a <code>String</code>.
IOException An IO error occurred writing the node to a byte array.

Return

String The String representation of the given DOM node.

Declaration

public static String serializeNode(Node node) throws TransformerException, IOException 

Method Source Code

//package com.java2s;
/*// w  w w .j  a  v  a 2 s  .  c o m
 * Copyright 2003 Federal Chancellery Austria
 * MOA-ID has been developed in a cooperation between BRZ, the Federal
 * Chancellery Austria - ICT staff unit, and Graz University of Technology.
 *
 * Licensed under the EUPL, Version 1.1 or - as soon they will be approved by
 * the European Commission - subsequent versions of the EUPL (the "Licence");
 * You may not use this work except in compliance with the Licence.
 * You may obtain a copy of the Licence at:
 * http://www.osor.eu/eupl/
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the Licence is distributed on an "AS IS" basis,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the Licence for the specific language governing permissions and
 * limitations under the Licence.
 *
 * This product combines work with different licenses. See the "NOTICE" text
 * file for details on the various modules and licenses.
 * The "NOTICE" text file is part of the distribution. Any derivative works
 * that you distribute must include a readable copy of the "NOTICE" text file.
 */

import java.io.ByteArrayOutputStream;
import java.io.IOException;

import javax.xml.transform.OutputKeys;

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 {
    /**
     * Serialize the given DOM node.
     * 
     * The node will be serialized using the UTF-8 encoding.
     * 
     * @param node The node to serialize.
     * @return String The <code>String</code> representation of the given DOM
     * node.
     * @throws TransformerException An error occurred transforming the
     * node to a <code>String</code>.
     * @throws IOException An IO error occurred writing the node to a byte array.
     */
    public static String serializeNode(Node node) throws TransformerException, IOException {
        return new String(serializeNode(node, "UTF-8", false), "UTF-8");
    }

    /**
     * Serialize the given DOM node.
     * 
     * The node will be serialized using the UTF-8 encoding.
     * 
     * @param node The node to serialize.
     * @param omitXmlDeclaration The boolean value for omitting the XML Declaration.
     * @return String The <code>String</code> representation of the given DOM
     * node.
     * @throws TransformerException An error occurred transforming the
     * node to a <code>String</code>.
     * @throws IOException An IO error occurred writing the node to a byte array.
     */
    public static String serializeNode(Node node, boolean omitXmlDeclaration)
            throws TransformerException, IOException {
        return new String(serializeNode(node, "UTF-8", omitXmlDeclaration), "UTF-8");
    }

    /**
     * Serialize the given DOM node.
     * 
     * The node will be serialized using the UTF-8 encoding.
     * 
     * @param node The node to serialize.
     * @param omitXmlDeclaration The boolean value for omitting the XML Declaration.
     * @param lineSeperator Sets the line seperator String of the parser
     * @return String The <code>String</code> representation of the given DOM
     * node.
     * @throws TransformerException An error occurred transforming the
     * node to a <code>String</code>.
     * @throws IOException An IO error occurred writing the node to a byte array.
     */
    public static String serializeNode(Node node, boolean omitXmlDeclaration, String lineSeperator)
            throws TransformerException, IOException {
        return new String(serializeNode(node, "UTF-8", omitXmlDeclaration, lineSeperator), "UTF-8");
    }

    /**
     * Serialize the given DOM node to a byte array.
     * 
     * @param node The node to serialize.
     * @param xmlEncoding The XML encoding to use.
     * @return The serialized node, as a byte array. Using a compatible encoding
     * this can easily be converted into a <code>String</code>.
     * @throws TransformerException An error occurred transforming the node to a 
     * byte array.
     * @throws IOException An IO error occurred writing the node to a byte array.
     */
    public static byte[] serializeNode(Node node, String xmlEncoding) throws TransformerException, IOException {
        return serializeNode(node, xmlEncoding, false);
    }

    /**
     * Serialize the given DOM node to a byte array.
     * 
     * @param node The node to serialize.
     * @param xmlEncoding The XML encoding to use.
     * @param omitDeclaration The boolean value for omitting the XML Declaration.
     * @return The serialized node, as a byte array. Using a compatible encoding
     * this can easily be converted into a <code>String</code>.
     * @throws TransformerException An error occurred transforming the node to a 
     * byte array.
     * @throws IOException An IO error occurred writing the node to a byte array.
     */
    public static byte[] serializeNode(Node node, String xmlEncoding, boolean omitDeclaration)
            throws TransformerException, IOException {
        return serializeNode(node, xmlEncoding, omitDeclaration, null);
    }

    /**
     * Serialize the given DOM node to a byte array.
     * 
     * @param node The node to serialize.
     * @param xmlEncoding The XML encoding to use.
     * @param omitDeclaration The boolean value for omitting the XML Declaration.
     * @param lineSeperator Sets the line seperator String of the parser
     * @return The serialized node, as a byte array. Using a compatible encoding
     * this can easily be converted into a <code>String</code>.
     * @throws TransformerException An error occurred transforming the node to a 
     * byte array.
     * @throws IOException An IO error occurred writing the node to a byte array.
     */
    public static byte[] serializeNode(Node node, String xmlEncoding, boolean omitDeclaration, String lineSeperator)
            throws TransformerException, IOException {

        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = transformerFactory.newTransformer();
        ByteArrayOutputStream bos = new ByteArrayOutputStream(16384);

        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty(OutputKeys.ENCODING, xmlEncoding);
        String omit = omitDeclaration ? "yes" : "no";
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, omit);
        if (null != lineSeperator) {
            transformer.setOutputProperty("{http://xml.apache.org/xalan}line-separator", lineSeperator);//does not work for xalan <= 2.5.1
        }
        transformer.transform(new DOMSource(node), new StreamResult(bos));

        bos.flush();
        bos.close();

        return bos.toByteArray();
    }
}

Related

  1. serializeElement(Element element)
  2. serializeElement(Element element, boolean omitXmlDeclaration, boolean indent, String method)
  3. serializeNode(Node node)
  4. serializeNode(Node node)
  5. serializeNode(Node node)
  6. serializeNode(Node node)
  7. serializeNode(Node node, String filename)
  8. serializeNode(Node node, String indentLevel, StringWriter writer)
  9. serializeNode(Node xmlNode)