Pretty Print XML with Java Transformer and XSLT
May 06, 2023
On this page, we will create Java application to produce pretty print XML with Java Transformer
and XSLT.
1. Java
Transformer
is an abstract class whose instance can transform a source tree into a result tree.
2. The
Transformer
is instantiated using TransformerFactory.newTransformer
method.
TransformerFactory transformerFactory = TransformerFactory.newInstance(); Transformer transformer = transformerFactory.newTransformer();
Transformer
using transform
method, can process XML from a variety of sources and write the transformation output to a variety of sinks.
void transform(Source xmlSource, Result outputTarget) throws TransformerException
Source
interface, contains the source XML input.
The Source
has following implementations.
DOMSource: XML source as DOM.
SAXSource: XML source as SAX-style.
StAXSource: XML source as StAX reader.
StreamSource: XML source as stream of XML markup.
In our example, we will use
StreamSource
class.
Source source = new StreamSource(new File("src/main/resources/inputFile.xml"));
Result
interface, contains the result tree. The Result
interface has following implementations.
DOMResult: Result tree in the form of DOM tree.
SAXResult: Result in SAX-style.
StAXResult: Result in the form of a StAX writer.
StreamResult: Result in the form of markup.
In our example, we will use
StreamResult
class.
Result result = new StreamResult(new File("src/main/resources/outputFile.xml"));
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
Pretty Print with Default Configurations
Find the source xml that needs to pretty print.inputFile.xml
<person created="20023-03-12T15:20" modified="20023-04-14T16:25"><firstName>Mahesh</firstName><lastName>Sharma</lastName><age>25</age> <address type="home"><village>XYZ Pur</village><city>Varanasi</city><state>UP</state><postalCode>123456</postalCode></address></person>
INDENT
output property as true.
Source source = new StreamSource(new File("src/main/resources/inputFile.xml")); Result result = new StreamResult(new File("src/main/resources/outputFile.xml")); TransformerFactory transformerFactory = TransformerFactory.newInstance(); try { Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(source, result); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); }
Pretty Print By Setting Output Properties
To set output properties,Transformer
has following methods.
void setOutputProperties(Properties format) void setOutputProperty(String name, String value)
transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(OutputKeys.INDENT, "yes");
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
TransformerFactory
using following method.
void setAttribute(String name, Object value)
transformerFactory.setAttribute("indent-number", 3);
Source source = new StreamSource(new File("src/main/resources/inputFile.xml")); Result result = new StreamResult(new File("src/main/resources/outputFile.xml")); TransformerFactory transformerFactory = TransformerFactory.newInstance(); transformerFactory.setAttribute("indent-number", 3); try { Transformer transformer = transformerFactory.newTransformer(); transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8"); transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.transform(source, result); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); }
outputFile.xml
<?xml version="1.0" encoding="UTF-8"?> <person created="20023-03-12T15:20" modified="20023-04-14T16:25"> <firstName>Mahesh</firstName> <lastName>Sharma</lastName> <age>25</age> <address type="home"> <village>XYZ Pur</village> <city>Varanasi</city> <state>UP</state> <postalCode>123456</postalCode> </address> </person>
Pretty Print with XSLT
Find the XSLT file.person-format.xslt
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <!-- Pretty Print Output --> <xsl:output method="xml" indent="yes" encoding="UTF-8"/> <!-- Match any element or attribute --> <xsl:template match="node()|@*"> <xsl:copy> <xsl:apply-templates select="node()|@*"/> </xsl:copy> </xsl:template> </xsl:stylesheet>
Source source = new StreamSource(new File("src/main/resources/inputFile.xml")); Result result = new StreamResult(new File("src/main/resources/outputFile.xml")); Source xsltSource = new StreamSource(new File("src/main/resources/person-format.xslt")); TransformerFactory transformerFactory = TransformerFactory.newInstance(); try { Transformer transformer = transformerFactory.newTransformer(xsltSource); transformer.transform(source, result); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); }
<xsl:output>
can have following attributes.
method="xml" | "html" | "text" version=STRING encoding=STRING omit-xml-declaration="yes" | "no" standalone="yes" | "no" doctype-public=STRING doctype-system=STRING cdata-section-elements=LIST-OF-NAMES indent="yes" | "no" media-type=STRING
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <!-- Pretty Print Output --> <xsl:output method="xml" indent="yes" omit-xml-declaration="yes" encoding="UTF-8"/> ------ </xsl:stylesheet>