Java XML Format formatXML(String unformatted)

Here you can find the source of formatXML(String unformatted)

Description

format XML

License

Open Source License

Declaration

public static String formatXML(String unformatted)
            throws SAXException, IOException, TransformerException, ParserConfigurationException 

Method Source Code


//package com.java2s;
/*/*ww w. java2 s.c o  m*/
 * #%L
 * OpenNaaS :: Core :: Resources
 * %%
 * Copyright (C) 2007 - 2014 Fundaci? Privada i2CAT, Internet i Innovaci? a Catalunya
 * %%
 * This program 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, either version 3 of the
 * License, or (at your option) any later version.
 * 
 * This program 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 General Lesser Public License for more details.
 * 
 * You should have received a copy of the GNU General Lesser Public
 * License along with this program.  If not, see
 * <http://www.gnu.org/licenses/lgpl-3.0.html>.
 * #L%
 */

import java.io.IOException;
import java.io.StringReader;
import java.io.StringWriter;
import javax.xml.parsers.ParserConfigurationException;
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 org.xml.sax.SAXException;

public class Main {
    public static String formatXML(String unformatted)
            throws SAXException, IOException, TransformerException, ParserConfigurationException {

        if (unformatted == null)
            return null;

        // remove whitespaces between xml tags
        String unformattedNoWhiteSpaces = unformatted.toString().replaceAll(">[\\s]+<", "><");

        // Instantiate transformer input
        Source xmlInput = new StreamSource(new StringReader(unformattedNoWhiteSpaces));
        StreamResult xmlOutput = new StreamResult(new StringWriter());

        // Configure transformer
        Transformer transformer = TransformerFactory.newInstance().newTransformer();
        transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        transformer.setOutputProperty(OutputKeys.INDENT, "yes");
        transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");

        transformer.transform(xmlInput, xmlOutput);
        String formatted = xmlOutput.getWriter().toString();

        return formatted;
    }
}

Related

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