Java Utililty Methods XML Format

List of utility methods to do XML Format

Description

The list of methods to do XML Format are organized into topic(s).

Method

StringformatXml(String xml)
format Xml
ByteArrayOutputStream output = new ByteArrayOutputStream();
try {
    readableXformer.transform(new StreamSource(new StringReader(xml)), new StreamResult(output));
} catch (TransformerException e) {
    throw new RuntimeException("Failed to format XML", e);
return output.toString();
StringformatXML(String xml)
format XML
try {
    Transformer serializer = SAXTransformerFactory.newInstance().newTransformer();
    serializer.setOutputProperty(OutputKeys.INDENT, "yes");
    serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
    Source xmlSource = new SAXSource(new InputSource(new ByteArrayInputStream(xml.getBytes())));
    StreamResult res = new StreamResult(new ByteArrayOutputStream());
    serializer.transform(xmlSource, res);
    return new String(((ByteArrayOutputStream) res.getOutputStream()).toByteArray());
...
TransformerformatXML(Transformer transformer)
This method formats the XML file by omitting the XML Declaration and creating indentations
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
return transformer;
StringformatXMLStr(String xml)
format XML Str
String output = null;
try {
    Node document = DocumentBuilderFactory.newInstance().newDocumentBuilder()
            .parse(new ByteArrayInputStream(xml.getBytes())).getDocumentElement();
    TransformerFactory tFactory = TransformerFactory.newInstance();
    Transformer transformer = tFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
...
StringprettifyString(String string)
Returns a prettified version of the string, or the original string if the operation is not possible.
String result = string;
if (string.startsWith("<?xml")) {
    result = prettifyXML(string);
return result;
StringprettyFormat(String input)
pretty Format
return prettyFormat(input, 2, true);
StringprettyFormat(String input)
Converts xml string to pretty format.
Source xmlInput = new StreamSource(new StringReader(input));
StringWriter stringWriter = new StringWriter();
StreamResult xmlOutput = new StreamResult(stringWriter);
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = null;
try {
    transformer = transformerFactory.newTransformer();
} catch (TransformerConfigurationException e) {
...
StringprettyFormat(String input)
Format the provided XML input with a default indentation of 2.
return prettyFormat(input, 2);
StringprettyFormat(String input, int indent)
pretty Format
try {
    Source xmlInput = new StreamSource(new StringReader(input));
    StringWriter stringWriter = new StringWriter();
    StreamResult xmlOutput = new StreamResult(stringWriter);
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    Transformer transformer = transformerFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
    transformer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", String.valueOf(indent));
...
StringprettyFormat(String strInput, int nIndent)
Pretty format a given XML document
try {
    Source xmlInput = new StreamSource(new StringReader(strInput));
    StringWriter stringWriter = new StringWriter();
    StreamResult xmlOutput = new StreamResult(stringWriter);
    TransformerFactory transformerFactory = TransformerFactory.newInstance();
    transformerFactory.setAttribute("indent-number", nIndent);
    Transformer transformer = transformerFactory.newTransformer();
    transformer.setOutputProperty(OutputKeys.INDENT, "yes");
...