Java XML Node to String toString(Node node, Map outputProperties)

Here you can find the source of toString(Node node, Map outputProperties)

Description

Converts an XML node to a string.

License

Open Source License

Parameter

Parameter Description
node the XML node
outputProperties the output properties

Return

the string

Declaration

public static String toString(Node node, Map<String, String> outputProperties) 

Method Source Code

//package com.java2s;

import java.io.StringWriter;
import java.io.Writer;

import java.util.HashMap;

import java.util.Map;

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.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;

import org.w3c.dom.Node;

public class Main {
    /**//from  w  ww  .  jav  a2  s  .  co  m
     * Converts an XML node to a string.
     * @param node the XML node
     * @return the string
     */
    public static String toString(Node node) {
        return toString(node, new HashMap<String, String>());
    }

    /**
     * Converts an XML node to a string.
     * @param node the XML node
     * @param prettyPrint true to pretty print, false not to
     * @return the string
     */
    public static String toString(Node node, boolean prettyPrint) {
        Map<String, String> properties = new HashMap<String, String>();
        if (prettyPrint) {
            properties.put(OutputKeys.INDENT, "yes");
            properties.put("{http://xml.apache.org/xslt}indent-amount", "2");
        }
        return toString(node, properties);
    }

    /**
     * Converts an XML node to a string.
     * @param node the XML node
     * @param outputProperties the output properties
     * @return the string
     */
    public static String toString(Node node, Map<String, String> outputProperties) {
        try {
            StringWriter writer = new StringWriter();
            toWriter(node, writer, outputProperties);
            return writer.toString();
        } catch (TransformerException e) {
            //should never be thrown because we're writing to string
            throw new RuntimeException(e);
        }
    }

    /**
     * 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(Node node)
  2. toString(Node node)
  3. toString(Node node)
  4. toString(Node node, boolean formatted)
  5. toString(Node node, int level, boolean indent)
  6. toString(Node node, Map outputProperties)
  7. toString(Node node, StringBuilder sb)
  8. toString(Node xml)
  9. toString(NodeList nodes)