se.kb.xml.XmlSerializer.java Source code

Java tutorial

Introduction

Here is the source code for se.kb.xml.XmlSerializer.java

Source

/*
 * Copyright 2008 National Library of Sweden Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License. You may obtain a copy of the License at
 * http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing,
 * software distributed under the License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS
 * OF ANY KIND, either express or implied. See the License for the specific language governing permissions and
 * limitations under the License.
 */

package se.kb.xml;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.io.OutputStreamWriter;

import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import com.google.common.base.Charsets;

/**
 * Utility class that has some static helper methods for handling XML.
 *
 * @author Oskar Grenholm, National Library of Sweden
 */
public final class XmlSerializer {

    private static final String ENCODING = Charsets.UTF_8.name();

    /** No need for a constructor. */
    private XmlSerializer() {
        // nothing to do here
    }

    /**
     * Writes the XML contained in the <code>Element</code> to the specified <code>OutputStream</code>.
     *
     * @param element the XML element to write
     * @param stream the stream to write to
     * @throws IOException
     */
    public static void writeXmlTo(Element element, OutputStream stream) throws IOException {
        OutputStreamWriter writer = new OutputStreamWriter(stream, Charsets.UTF_8);
        OutputFormat format = OutputFormat.createPrettyPrint();
        format.setEncoding(ENCODING);

        XMLWriter xmlwriter = new XMLWriter(writer, format);
        xmlwriter.write(element);
        xmlwriter.flush();
        writer.flush();
    }

    /**
     * Converts an <code>Element</code> to a <code>String</code>.
     *
     * @param xml the XML element to write
     * @return a <code>String</code> with the XML
     * @throws IOException
     */
    public static String xmlToString(Element xml) throws IOException {
        ByteArrayOutputStream stream = new ByteArrayOutputStream();
        writeXmlTo(xml, stream);
        return stream.toString(ENCODING);
    }
}