Java XML Node to String toWriter(Node node, Writer writer, Map outputProperties)

Here you can find the source of toWriter(Node node, Writer writer, Map outputProperties)

Description

Writes an XML node to a writer.

License

Open Source License

Parameter

Parameter Description
node the XML node
writer the writer
outputProperties the output properties

Exception

Parameter Description
TransformerException if there's a problem writing to the writer

Declaration

public static void toWriter(Node node, Writer writer, Map<String, String> outputProperties)
        throws TransformerException 

Method Source Code

//package com.java2s;

import java.io.Writer;

import java.util.HashMap;

import java.util.Map;

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

public class Main {
    /**//w  ww  . j av a  2  s  .  c o m
     * Writes an XML node to a writer.
     * @param node the XML node
     * @param writer the writer
     * @throws TransformerException if there's a problem writing to the writer
     */
    public static void toWriter(Node node, Writer writer) throws TransformerException {
        toWriter(node, writer, new HashMap<String, String>());
    }

    /**
     * Writes an XML node to a writer.
     * @param node the XML node
     * @param writer the writer
     * @param outputProperties the output properties
     * @throws TransformerException if there's a problem writing to the writer
     */
    public static void toWriter(Node node, Writer writer, Map<String, String> outputProperties)
            throws TransformerException {
        try {
            Transformer transformer = TransformerFactory.newInstance().newTransformer();
            for (Map.Entry<String, String> property : outputProperties.entrySet()) {
                try {
                    transformer.setOutputProperty(property.getKey(), property.getValue());
                } catch (IllegalArgumentException e) {
                    //ignore invalid output properties
                }
            }

            DOMSource source = new DOMSource(node);
            StreamResult result = new StreamResult(writer);
            transformer.transform(source, result);
        } catch (TransformerConfigurationException e) {
            //no complex configurations
        } catch (TransformerFactoryConfigurationError e) {
            //no complex configurations
        }
    }
}

Related

  1. toString(NodeList nodes)
  2. toStringE(Node element)
  3. toText(Node node)
  4. toText(Node node)
  5. toWellKnowName(Node vertexStyleNode)
  6. toXML(Node node)
  7. toXml(Node node)
  8. toXml(Node node)
  9. toXML(Node node)