Java XML Format formatXml(String xml)

Here you can find the source of formatXml(String xml)

Description

Takes given XML and indents everything with 2 spaces.

License

Open Source License

Parameter

Parameter Description
xml The XML to format in text format.

Exception

Parameter Description
TransformerException If it all breaks up.

Return

Same XML but formatted with indents of 2 spaces.

Declaration


public static String formatXml(String xml) throws TransformerException 

Method Source Code


//package com.java2s;
/*//from  w w w  .  j  a  v  a 2s. c om
 * Copyright (C) 2010-2011 VTT Technical Research Centre of Finland.
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation;
 * version 2.1 of the License.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
 */

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

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

public class Main {
    /**
     * Takes given XML and indents everything with 2 spaces.
     *
     * @param xml The XML to format in text format.
     * @return Same XML but formatted with indents of 2 spaces.
     * @throws TransformerException If it all breaks up.
     */
    //TODO: add tests
    public static String formatXml(String xml) throws TransformerException {
        try {
            Source xmlInput = new StreamSource(new StringReader(xml));
            StringWriter stringWriter = new StringWriter();
            StreamResult xmlOutput = new StreamResult(stringWriter);
            TransformerFactory transformerFactory = TransformerFactory.newInstance();
            transformerFactory.setAttribute("indent-number", 2);
            Transformer transformer = transformerFactory.newTransformer();
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.transform(xmlInput, xmlOutput);
            return xmlOutput.getWriter().toString();
        } catch (Exception e) {
            throw new RuntimeException(e); // simple exception handling, please review it
        }
    }

    /**
     * Creates a string representation for a stack trace for an exception.
     * For logging, failure analysis, etc.
     *
     * @param t The throwable for which to create the string.
     * @return The stack trace as string.
     */
    public static String toString(Throwable t) {
        StringWriter sw = new StringWriter();
        PrintWriter pw = new PrintWriter(sw);
        t.printStackTrace(pw);
        return sw.toString();
    }
}

Related

  1. format(String unformattedXml)
  2. formatAttributes(Node node)
  3. formattedPrint(Node xml, OutputStream out)
  4. formatXML(byte[] xmlData, int indent)
  5. formatXML(String unformatted)
  6. formatXml(String xml)
  7. formatXML(String xml)
  8. formatXML(Transformer transformer)
  9. formatXMLStr(String xml)