Java XML Format prettyFormat(String input)

Here you can find the source of prettyFormat(String input)

Description

Converts xml string to pretty format.

License

Apache License

Parameter

Parameter Description
input xml string to be converted to pretty format

Return

pretty format xml string

Declaration

static String prettyFormat(String input) 

Method Source Code


//package com.java2s;
/*/*from   www.j  ava 2 s. c  om*/
 * Copyright 2016-present Open Networking Foundation
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

import javax.xml.transform.OutputKeys;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
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.StringReader;
import java.io.StringWriter;

public class Main {
    /**
     * Converts xml string to pretty format.
     *
     * @param input xml string to be converted to pretty format
     * @return pretty format xml string
     */
    static String prettyFormat(String input) {
        // Prepare input and output stream
        Source xmlInput = new StreamSource(new StringReader(input));
        StringWriter stringWriter = new StringWriter();
        StreamResult xmlOutput = new StreamResult(stringWriter);

        // Create transformer
        TransformerFactory transformerFactory = TransformerFactory.newInstance();
        Transformer transformer = null;

        try {
            transformer = transformerFactory.newTransformer();
        } catch (TransformerConfigurationException e) {
            return "";
        }

        // Need to omit the xml header and set indent to 4
        if (transformer != null) {
            transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty("{http://xml.apache" + ".org/xslt}indent-amount", "4");

            // Covert input string to xml pretty format and return
            try {
                transformer.transform(xmlInput, xmlOutput);
            } catch (TransformerException e) {
                return "";
            }
        }
        return xmlOutput.getWriter().toString();
    }
}

Related

  1. formatXml(String xml)
  2. formatXML(Transformer transformer)
  3. formatXMLStr(String xml)
  4. prettifyString(String string)
  5. prettyFormat(String input)
  6. prettyFormat(String input)
  7. prettyFormat(String input, int indent)
  8. prettyFormat(String strInput, int nIndent)
  9. prettyFormatXml(final InputStream xml, final OutputStream os, final int indent)