Pretty Print XML with Java Transformer and XSLT

By Arvind Rai, 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(); 
3. The 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 
4. An object that implements 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"));
5. An object that implements the 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")); 
6. For pretty printing, we need to set INDENT output property.
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> 
Find the pretty print example with default configurations. We only need to set 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) 
In our example, we are setting following output properties.
transformer.setOutputProperty(OutputKeys.DOCTYPE_PUBLIC, "yes");	  
transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
transformer.setOutputProperty(OutputKeys.INDENT, "yes"); 
To omit XML declaration, we set following output property.
transformer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
We can also set attributes to TransformerFactory using following method.
void setAttribute(String name, Object value) 
Set indent number as following.
transformerFactory.setAttribute("indent-number", 3);
Now find the example.
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();
} 
Find the output as pretty print XML.
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> 
Find the example.
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();
} 
The <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 
For Example:
<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> 

Reference

POSTED BY
ARVIND RAI
ARVIND RAI
LEARN MORE








©2024 concretepage.com | Privacy Policy | Contact Us