Why Pretty-Print XML in Java?
XML files can quickly become difficult to read in their raw form. Whether you're handling configuration files or processing XML responses, a neatly formatted structure can significantly improve clarity and debugging efforts.
Using the Transformer Class in Java
Java provides the Transformer class to easily transform and format XML documents. Here's a simple method to pretty-print XML in your Java application, including control over indent size and XML declaration handling.
public static String prettyPrintByTransformer(String xmlString, int indent, boolean ignoreDeclaration) {try {InputSource src = new InputSource(new StringReader(xmlString));Document document = DocumentBuilderFactory.newInstance().newDocumentBuilder().parse(src);TransformerFactory transformerFactory = TransformerFactory.newInstance();transformerFactory.setAttribute("indent-number", indent);Transformer transformer = transformerFactory.newTransformer();transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, ignoreDeclaration ? "yes" : "no");transformer.setOutputProperty(OutputKeys.INDENT, "yes");Writer out = new StringWriter();transformer.transform(new DOMSource(document), new StreamResult(out));return out.toString();} catch (Exception e) {throw new RuntimeException("Error occurs when pretty-printing xml:\n" + xmlString, e);}}
This method allows for customizable XML formatting in your Java program, providing a user-friendly way to display XML content with proper indenting.
Handling Pretty-Print in Java 9 and Above
From Java 9 onwards, the Transformer class introduced changes that could lead to extra whitespace in the output. To ensure consistent formatting across different Java versions, you can define a custom XSLT file for more precise control.
Pretty-Printing with the Dom4j Library
Alternatively, you can use the Dom4j library, which simplifies the process of pretty-printing XML. It offers powerful features and flexible options for formatting, including easy control over indentation and declaration output.
public static String prettyPrintByDom4j(String xmlString, int indent, boolean skipDeclaration) {try {OutputFormat format = OutputFormat.createPrettyPrint();format.setIndentSize(indent);format.setSuppressDeclaration(skipDeclaration);format.setEncoding("UTF-8");org.dom4j.Document document = DocumentHelper.parseText(xmlString);StringWriter sw = new StringWriter();XMLWriter writer = new XMLWriter(sw, format);writer.write(document);return sw.toString();} catch (Exception e) {throw new RuntimeException("Error occurs when pretty-printing xml:\n" + xmlString, e);}}
With Dom4j, pretty-printing XML becomes a breeze, even for complex XML structures. Adding this library to your project can streamline your XML processing tasks.
Conclusion
Whether using Java's built-in Transformer class or the Dom4j library, pretty-printing XML can significantly enhance the readability of your XML files. At Jimni Nomics, we offer tools and solutions to help you achieve clean, formatted XML in your Java applications.