Java XML Node Serialize serialize(Node node, File outdir, String fileName)

Here you can find the source of serialize(Node node, File outdir, String fileName)

Description

Serialize the supplied DOM node to the specified file in the specified output directory.

License

Open Source License

Parameter

Parameter Description
node The DOM node to be serialised.
outdir The directory into which the file is to be serialised.
fileName The name of the file.

Exception

Parameter Description
ConfigurationException Unable to serialise the node.

Declaration

public static void serialize(Node node, File outdir, String fileName) throws ConfigurationException 

Method Source Code

//package com.java2s;
// License as published by the Free Software Foundation; either

import java.io.File;

import java.io.OutputStream;

import javax.naming.ConfigurationException;

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.Node;

public class Main {
    /**//from w w  w . j  a  v  a  2 s  .  co m
     * Serialize the supplied DOM node to the specified file in the specified output directory.
     * @param node The DOM node to be serialised.
     * @param outdir The directory into which the file is to be serialised.
     * @param fileName The name of the file.
     * @throws ConfigurationException Unable to serialise the node.
     */
    public static void serialize(Node node, File outdir, String fileName) throws ConfigurationException {
        serialize(node, new StreamResult(new File(outdir, fileName)));
    }

    public static void serialize(Node node, OutputStream out) throws ConfigurationException {
        serialize(node, new StreamResult(out));
    }

    /**
      * Serialize the supplied DOM node to the supplied DOM StreamResult instance.
      * @param node The DOM node to be serialised.
      * @param streamRes The StreamResult into which the node is to be serialised.
      * @throws ConfigurationException Unable to serialise the node.
      */
    public static void serialize(Node node, StreamResult streamRes) throws ConfigurationException {
        serialize(node, streamRes, false);
    }

    /**
     * Serialize the supplied DOM node to the supplied DOM StreamResult instance.
     * @param node The DOM node to be serialised.
     * @param streamRes The StreamResult into which the node is to be serialised.
      * @param omitXmlDecl Omit the XML declaration.
     * @throws ConfigurationException Unable to serialise the node.
     */
    public static void serialize(Node node, StreamResult streamRes, boolean omitXmlDecl)
            throws ConfigurationException {
        DOMSource domSource = new DOMSource(node);

        try {
            Transformer transformer = TransformerFactory.newInstance().newTransformer();

            // There's a bug in Java 5 re this code (formatting).
            // See http://forum.java.sun.com/thread.jspa?threadID=562510&start=0 and it explains the
            // whys of the following code.
            // transformer.setOutputProperty("{http://xml.apache.org/xalan}indent-amount", "4");
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, (omitXmlDecl ? "yes" : "no"));
            transformer.transform(domSource, streamRes);
        } catch (Exception e) {
            throw new ConfigurationException("Failed to serialize ESB Configuration Document instance.");
        }
    }
}

Related

  1. serialize(Element e, boolean omitXMLDeclaration)
  2. serialize(Node doc)
  3. serialize(Node n, StreamResult sr)
  4. serialize(Node node)
  5. serialize(Node node)
  6. serialize(NodeList nodeList)
  7. serializeDom(Node domElement)
  8. serializeDom(Node node, StringBuffer writeString)
  9. serializeElement(Element element)