Java XML Node to String xmlToString(Node doc)

Here you can find the source of xmlToString(Node doc)

Description

format xml with 2 space indentation.

License

Apache License

Parameter

Parameter Description
doc Docment/Node containing the xml

Exception

Parameter Description
TransformerException the transformer exception
TransformerConfigurationException the transformer configuration exception
IOException Signals that an I/O exception has occurred.

Return

a formatted String

Declaration

public static String xmlToString(Node doc)
        throws TransformerException, TransformerConfigurationException, IOException 

Method Source Code

//package com.java2s;
//License from project: Apache License 

import java.io.BufferedReader;

import java.io.IOException;

import java.io.PrintWriter;
import java.io.StringReader;
import java.io.StringWriter;

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 org.w3c.dom.Node;

public class Main {
    /**//from w  w  w  . j a  v a  2 s .  com
     * format xml with 2 space indentation.
     * 
     * @param doc
     *            Docment/Node containing the xml
     * @return a formatted String
     * @throws TransformerException
     *             the transformer exception
     * @throws TransformerConfigurationException
     *             the transformer configuration exception
     * @throws IOException
     *             Signals that an I/O exception has occurred.
     */
    public static String xmlToString(Node doc)
            throws TransformerException, TransformerConfigurationException, IOException {
        // format the xml into a String with 2 space indent
        DOMSource domSource = new DOMSource(doc);
        TransformerFactory tf = TransformerFactory.newInstance();
        Transformer transformer = tf.newTransformer();
        transformer.setOutputProperty(OutputKeys.METHOD, "xml");
        transformer.setOutputProperty("{http://xml.apache.org/xalan}indent-amount", "2");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("encoding", "UTF-8");

        StringWriter sw = new StringWriter();
        StreamResult sr = new StreamResult(sw);
        transformer.transform(domSource, sr);

        // strip out blank lines
        BufferedReader br = new BufferedReader(new StringReader(sw.toString()));
        StringWriter sw1 = new StringWriter();
        PrintWriter bw = new PrintWriter(sw1);
        String line = null;
        while ((line = br.readLine()) != null) {
            if (line.trim().length() > 0)
                bw.println(line);
        }
        return sw1.toString();
    }
}

Related

  1. xmlToBytes(final Node body)
  2. XmlToSource(Node node)
  3. xmlToStream(Node n, OutputStream os)
  4. xmlToStreamE(Node n, OutputStream os)
  5. xmlToString(Node doc)
  6. xmlToString(Node node)
  7. xmlToString(Node node)
  8. xmlToString(Node node)
  9. xmlToString(Node node)