Java XML Node Serialize serialize(Element e, boolean omitXMLDeclaration)

Here you can find the source of serialize(Element e, boolean omitXMLDeclaration)

Description

Serialize an XML Element into a String.

License

Open Source License

Parameter

Parameter Description
e Element to be serialized.
omitXMLDeclaration boolean representing whether or not to omit the XML declaration.

Return

String representation of the XML document fragment.

Declaration

public static final String serialize(Element e, boolean omitXMLDeclaration) 

Method Source Code

//package com.java2s;
/*******************************************************************************
 * Copyright (c) 2004, 2005 IBM Corporation and others.
 * 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:/* ww  w .j av  a2 s.  c o  m*/
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/

import java.io.ByteArrayOutputStream;

import javax.xml.transform.OutputKeys;
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.DocumentFragment;
import org.w3c.dom.Element;

public class Main {
    public static final String UTF8_ENCODING = "UTF-8";

    /**
     * Serialize an XML Element into a String.
     * @param e Element to be serialized.
     * @param omitXMLDeclaration boolean representing whether or not to omit the XML declaration.
     * @return String representation of the XML document fragment.
     */
    public static final String serialize(Element e, boolean omitXMLDeclaration) {
        if (e != null) {
            try {
                DOMSource domSource = new DOMSource(e);
                Transformer serializer = TransformerFactory.newInstance().newTransformer();
                serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,
                        ((omitXMLDeclaration) ? "yes" : "no"));
                serializer.setOutputProperty(OutputKeys.INDENT, "yes");
                serializer.setOutputProperty(OutputKeys.ENCODING, UTF8_ENCODING);
                serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                serializer.transform(domSource, new StreamResult(baos));
                baos.close();
                return new String(baos.toByteArray(), UTF8_ENCODING);
            } catch (Throwable t) {
            }
        }
        return null;
    }

    /**
     * Serialize an XML Element into a String.
     * @param df DocumentFragment to be serialized.
     * @param omitXMLDeclaration boolean representing whether or not to omit the XML declaration.
     * @return String representation of the XML document fragment.
     */
    public static final String serialize(DocumentFragment df, boolean omitXMLDeclaration) {
        if (df != null) {
            try {
                DOMSource domSource = new DOMSource(df);
                Transformer serializer = TransformerFactory.newInstance().newTransformer();
                serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION,
                        ((omitXMLDeclaration) ? "yes" : "no"));
                serializer.setOutputProperty(OutputKeys.INDENT, "yes");
                serializer.setOutputProperty(OutputKeys.ENCODING, UTF8_ENCODING);
                serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
                ByteArrayOutputStream baos = new ByteArrayOutputStream();
                serializer.transform(domSource, new StreamResult(baos));
                baos.close();
                return new String(baos.toByteArray(), UTF8_ENCODING);
            } catch (Throwable t) {
            }
        }
        return null;
    }
}

Related

  1. getSerializer(final File file)
  2. serialise(Node node)
  3. serialiseBytes(Node n)
  4. serialize(Node doc)
  5. serialize(Node n, StreamResult sr)
  6. serialize(Node node)
  7. serialize(Node node)