Java XML Document Serialize serialize(Document document, String fileName)

Here you can find the source of serialize(Document document, String fileName)

Description

Serializes the XML document to a file.

License

Apache License

Parameter

Parameter Description
document Document or null
fileName Name of the file to serialize into

Declaration

public static void serialize(Document document, String fileName) throws IOException 

Method Source Code

//package com.java2s;
/*//from w  ww . j  a  v a 2s. c  o m
 *   Copyright 2007 skynamics AG
 *
 *   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.
 */

import java.io.ByteArrayOutputStream;
import java.io.File;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;

public class Main {
    /**
     * Serializes the XML document to a file.
     *
     * @param document Document or null
     * @param fileName Name of the file to serialize into
     * @exception IOException Error on converting or writing file
     */
    public static void serialize(Document document, String fileName) throws IOException {
        if (document == null)
            return;

        Exception targetException = null;

        // Use a XSLT transformer for writing the XML file
        Transformer transformer;
        try {
            transformer = TransformerFactory.newInstance().newTransformer();
            DOMSource source = new DOMSource(document);
            FileOutputStream os = new FileOutputStream(new File(fileName));
            StreamResult result = new StreamResult(os);
            transformer.transform(source, result);
        } catch (TransformerConfigurationException e) {
            targetException = e;
        } catch (FileNotFoundException e) {
            targetException = e;
        } catch (TransformerException e) {
            targetException = e;
        } catch (TransformerFactoryConfigurationError e) {
            throw new IOException("Error serializing XML data to file '" + fileName + "':" + e.toString());
        }

        if (targetException != null) {
            throw new IOException(
                    "Error serializing XML data to file '" + fileName + "': " + targetException.getMessage());
        }
    }

    /**
     * Serializes the XML node tree to a string.
     *
     * @param document XML node or null
     * @return XML string or null on error or if document is null
     * @exception IOException Error on converting
     */
    public static String serialize(Document document) throws IOException {
        if (document == null)
            return null;

        Exception targetException = null;

        // Use a XSLT transformer for writing the XML file
        Transformer transformer;
        try {
            transformer = TransformerFactory.newInstance().newTransformer();
            DOMSource source = new DOMSource(document);
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            StreamResult result = new StreamResult(os);
            transformer.transform(source, result);
            return os.toString();
        } catch (TransformerConfigurationException e) {
            targetException = e;
        } catch (TransformerException e) {
            targetException = e;
        } catch (TransformerFactoryConfigurationError e) {
            throw new IOException("Error serializing XML data:" + e.toString());
        }

        if (targetException != null) {
            throw new IOException("Error serializing XML data: " + targetException.getMessage());
        }

        return null;
    }
}

Related

  1. serialize(Document document, boolean prettyPrint)
  2. serialize(Document document, boolean prettyPrint)
  3. serialize(Document document, File targetFile)
  4. serialize(Document document, OutputStream ostream)
  5. serialize(Document document, OutputStream out)
  6. serialize(Document document, Writer writer)
  7. serialize(final Document doc, final OutputStream os, final String encoding)
  8. serialize(final Document document, final OutputStream out)
  9. serializeDocument(Document doc)