Java XML Document to String toByteArray(final Document document)

Here you can find the source of toByteArray(final Document document)

Description

To byte array.

License

Open Source License

Parameter

Parameter Description
document the document

Return

the byte[]

Declaration

public static byte[] toByteArray(final Document document) 

Method Source Code

//package com.java2s;
/***********************************************************************************************************************
 * Copyright (c) 2008 empolis GmbH and brox IT Solutions GmbH. All rights reserved. This program and the accompanying
 * materials are made available under the terms of the Eclipse Public License v1.0 which accompanies this distribution,
 * and is available at http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors: Ivan Churkin (brox IT Solutions GmbH) - initial creator
 *               Sebastian Voigt (brox IT Solutions GmbH)
 *               Andreas Weber (empolis GmbH) - switch to XML 1.1 in header to make more documents readable.
 **********************************************************************************************************************/

import java.io.StringWriter;

import javax.xml.transform.OutputKeys;
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 {
    /**/*from www .j  a  va  2  s.co m*/
     * The Constant TRANSFORMER_FACTORY.
     */
    public static final TransformerFactory TRANSFORMER_FACTORY = TransformerFactory.newInstance();
    /**
     * The Constant XML_HEADER_UTF8.
     */
    public static final String XML_HEADER_UTF8 = "<?xml version=\"1.1\" encoding=\"UTF-8\"?>";

    /**
     * To byte array.
     *
     * @param document
     *          the document
     *
     * @return the byte[]
     */
    public static byte[] toByteArray(final Document document) {
        if (document == null) {
            return null;
        }
        // force to UTF-8, BOS is not used.
        try {
            final String string = XML_HEADER_UTF8 + toString(document);
            return string.getBytes("utf-8");
        } catch (final Exception e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * To string.
     *
     * @param document
     *          the document
     *
     * @return the string
     */
    public static String toString(final Document document) {
        if (document == null) {
            return null;
        }
        // final StringWriter xmlString = new StringWriter();
        // final OutputFormat format = new OutputFormat(document);
        // final XMLSerializer serializer = new XMLSerializer(xmlString, format);
        // serializer.serialize(document);
        // final String xml = xmlString.toString();
        // return xml;
        return toString(document, false);
    }

    /**
     * To string.
     *
     * @param document
     *          the document
     * @param xmlDeclaration
     *          the xml declaration
     *
     * @return the string
     */
    public static String toString(final Document document, final boolean xmlDeclaration) {
        if (document == null) {
            return null;
        }
        final StreamResult streamResult = new StreamResult(new StringWriter());
        transform(document, streamResult, xmlDeclaration);
        final String stringXml = streamResult.getWriter().toString();
        return stringXml;
    }

    /**
     * Transform.
     *
     * @param document
     *          the document
     * @param streamResult
     *          the stream result
     * @param xmlDeclaration
     *          the xml declaration
     */
    public static void transform(final Document document, final StreamResult streamResult,
            final boolean xmlDeclaration) {
        if (document == null) {
            throw new IllegalArgumentException("document cannot be NULL!");
        }
        if (streamResult == null) {
            throw new IllegalArgumentException("streamResult cannot be NULL!");
        }
        Transformer transformer;
        try {
            transformer = TRANSFORMER_FACTORY.newTransformer();
        } catch (final TransformerConfigurationException e) {
            throw new RuntimeException(e);
        }
        transformer.setOutputProperty(OutputKeys.INDENT, "no");
        String strXmlDeclaration;
        if (xmlDeclaration) {
            strXmlDeclaration = "no";
        } else {
            strXmlDeclaration = "yes";
        }
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, strXmlDeclaration);
        final DOMSource source = new DOMSource(document);
        try {
            transformer.transform(source, streamResult);
        } catch (final TransformerException e) {
            throw new RuntimeException(e);
        }
    }
}

Related

  1. getStringListFromXPath(Document doc, XPath xpath, String rootNodeExpression, String listExpression)
  2. getStringRepresentation(DocumentFragment df)
  3. getXML(Document doc)
  4. getXml(Document doc)
  5. getXML(Document pDocument)
  6. toByteArray(final Document document)
  7. toStream(Document document, OutputStream stream)
  8. toString(Document d)
  9. toString(Document doc)