Java XML Node to String toXML(Node node)

Here you can find the source of toXML(Node node)

Description

Takes a Node object and converts it to a String representing the valid XML structure.

License

Open Source License

Parameter

Parameter Description
node Node to show as String

Return

The as

Declaration

public static final String toXML(Node node) throws TransformerFactoryConfigurationError, TransformerException 

Method Source Code


//package com.java2s;
//License from project: Open Source License 

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

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
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  w  w .j  a v  a2 s.c om
     * Takes a {@link Node} object and converts it to a {@link String} representing the valid XML structure.
     * 
     * @param node
     *            {@link Node} to show as {@link String}
     * @return The {@link Node} as {@link String}
     */
    public static final String toXML(Node node) throws TransformerFactoryConfigurationError, TransformerException {
        Transformer tf = TransformerFactory.newInstance().newTransformer();
        tf.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
        tf.setOutputProperty(OutputKeys.INDENT, "yes");
        tf.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        tf.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        Writer out = new StringWriter();
        tf.transform(new DOMSource(node), new StreamResult(out));
        return out.toString();
    }
}

Related

  1. toText(Node node)
  2. toText(Node node)
  3. toWellKnowName(Node vertexStyleNode)
  4. toWriter(Node node, Writer writer, Map outputProperties)
  5. toXML(Node node)
  6. toXml(Node node)
  7. toXml(Node node)
  8. toXml(Node node, boolean keepHeader)
  9. toXMLString(final Node value)