Convert XML to HTML in Java using XSLT example

In this tutorial, let us convert XML to HTML using XSLT (Extensible Stylesheet Language Transformations) language. It is very simple to convert XML to any other formats such as HTML, plain text, etc.. using XSLT. For generating XML from table data, you can visit my earlier tutorial Java to convert table data to XML and vice versa.

Now let us convert the below XML to html using XSLT.

XML:

 

	<ProductList>
		<Product>
			<productId>I1</productId>
			<productName>Samsung LED Tv</productName>
			<price>40000.00</price>
			<stock>5</stock>
		</Product>
		<Product>
			<productId>I3</productId>
			<productName>SONY LCD TV</productName>
			<price>30000.00</price>
			<stock>7</stock>

		</Product>
	</ProductList>

Store the XML in a file called product.xml

Now let us create XSLT for generating html for producing the below output.

html output

XSLT:

 

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
	version="1.0">

	<xsl:template match="/">
		<html>

			<head>
				<style type="text/css">
					table.tfmt {
					border: 1px ;
					}

					td.colfmt {
					border: 1px ;
					background-color: white;
					color: black;
					text-align:right;
					}

					th {
					background-color: #2E9AFE;
					color: white;
					}

				</style>
			</head>

			<body>
				<table class="tfmt">
					<tr>
						<th style="width:250px">Product Code:</th>
						<th style="width:350px">Product Name:</th>
						<th style="width:250px">Price:</th>
						<th style="width:250px">Stock:</th>


					</tr>

					<xsl:for-each select="ProductList/Product">

						<tr>
							<td class="colfmt">
								<xsl:value-of select="productId" />
							</td>
							<td class="colfmt">
								<xsl:value-of select="productName" />
							</td>

							<td class="colfmt">
								<xsl:value-of select="price" />
							</td>
							<td class="colfmt">
								<xsl:value-of select="stock" />
							</td>
						</tr>

					</xsl:for-each>
				</table>
			</body>
		</html>
	</xsl:template>
</xsl:stylesheet>

Store the above xslt in a file called product.xsl

Now let us write Java code for converting the above XML to HTML.

XmlToHtml.java

 

package net.javaonline.db2xml;

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.StringWriter;

import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.TransformerFactoryConfigurationError;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

public class XmlToHtml {

	public static void main(String args[]) {
		Source xml = new StreamSource(new File("D:\\template\\product.xml"));
		Source xslt = new StreamSource("D:\\template\\product.xsl");

		convertXMLToHTML(xml, xslt);

	}

	public static void convertXMLToHTML(Source xml, Source xslt) {
		StringWriter sw = new StringWriter();

		try {

			FileWriter fw = new FileWriter("D:\\template\\product.html");
			TransformerFactory tFactory = TransformerFactory.newInstance();
			Transformer trasform = tFactory.newTransformer(xslt);
			trasform.transform(xml, new StreamResult(sw));
			fw.write(sw.toString());
			fw.close();

			System.out
					.println("product.html generated successfully at D:\\template ");

		} catch (IOException | TransformerConfigurationException e) {
			e.printStackTrace();
		} catch (TransformerFactoryConfigurationError e) {
			e.printStackTrace();
		} catch (TransformerException e) {
			e.printStackTrace();
		}

		
	}

In the above java program, the method convertXMLToHTML accepts two arguments i.e xml and xslt. Create an instance of Transformer class using TransformerFactory.newTransformer method based on xslt. Use transform method of the transformer object to convert the input XML to any other format like html.

Running the above program will generate the product.html at the folder d:\template

Running the html will output the following result.

html output

Source: https://docs.oracle.com/javase/7/docs/api/javax/xml/transform/Transformer.html

Leave a Reply