Java XML Format formatXml(String xml)

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

Description

format Xml

License

Apache License

Declaration

public static synchronized String formatXml(String xml) 

Method Source Code

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

import java.io.ByteArrayOutputStream;
import java.io.StringReader;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;

import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

public class Main {
    private static Transformer readableXformer;

    public static synchronized String formatXml(String xml) {

        ByteArrayOutputStream output = new ByteArrayOutputStream();
        try {//from w w  w .  j av  a  2  s . c  o  m
            readableXformer.transform(new StreamSource(new StringReader(xml)), new StreamResult(output));
        } catch (TransformerException e) {
            throw new RuntimeException("Failed to format XML", e);
        }
        return output.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)