Java XML String Transform write(Node node, OutputStream out, String... props)

Here you can find the source of write(Node node, OutputStream out, String... props)

Description

Writes a node to an OutputStream .

License

Open Source License

Parameter

Parameter Description
node The node to write.
out The stream to write to.
props A list of zero or more option/value parameters, from OutputKeys .

Exception

Parameter Description
TransformerException The transformation failed.

Declaration

public static void write(Node node, OutputStream out, String... props) throws TransformerException 

Method Source Code

//package com.java2s;

import org.w3c.dom.Node;
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 java.io.OutputStream;

import java.io.Writer;

public class Main {
    private static TransformerFactory transformerFactory = null;

    /**//from   w  w  w  . java  2 s  .c  om
     * Writes a document to a {@link Writer}.
     *
     * @param node  The node to write.
     * @param out   The stream to write to.
     * @param props A list of zero or more option/value parameters, from {@link
     *              OutputKeys}.
     *
     * @throws TransformerException The transformation failed.
     */
    public static void write(Node node, Writer out, String... props) throws TransformerException {

        StreamResult streamResult = new StreamResult(out);
        write(node, streamResult, props);
    }

    /**
     * Writes a node to an {@link OutputStream}.
     *
     * @param node  The node to write.
     * @param out   The stream to write to.
     * @param props A list of zero or more option/value parameters, from {@link
     *              OutputKeys}.
     *
     * @throws TransformerException The transformation failed.
     */
    public static void write(Node node, OutputStream out, String... props) throws TransformerException {

        StreamResult streamResult = new StreamResult(out);
        write(node, streamResult, props);
    }

    private static void write(Node node, StreamResult streamResult, String... props) throws TransformerException {

        if (props.length % 2 != 0)
            throw new IllegalArgumentException("properties must be name/value pairs of strings");

        DOMSource domSource = new DOMSource(node);

        Transformer xmlOut = getTransformer();
        if (props.length == 0 || props[0].equals("+")) {
            xmlOut.setOutputProperty(OutputKeys.INDENT, "yes");
            xmlOut.setOutputProperty(OutputKeys.ENCODING, "ISO-8859-1");
        }
        for (int i = 0; i < props.length; i += 2)
            xmlOut.setOutputProperty(props[i], props[i + 1]);
        xmlOut.transform(domSource, streamResult);
    }

    private static Transformer getTransformer() throws TransformerConfigurationException {
        return getTransformerFactory().newTransformer();
    }

    private static synchronized TransformerFactory getTransformerFactory() {
        if (transformerFactory == null)
            transformerFactory = TransformerFactory.newInstance();
        return transformerFactory;
    }
}

Related

  1. toJson(String xml)
  2. toLCCNDisplay(String packedLCCN)
  3. tomcatPort(String fileName)
  4. toString(Collection c, String separator)
  5. toString(Source input)
  6. writeElement(Element element, String fileName)
  7. writeNodeSubtreeXMLString(final Node node, final Writer writer)
  8. writePropToString(Properties props)
  9. writeResultToFile(final StreamResult result, final String path)