XML to HTML Transformation

Below example will take SourceXML.xml(Source XML Block) and HTMLStyleSheet.xsl(StyleSheet to process XML Block) as input. On transform action StyleSheet will process given XML block and store result at Employees.html file.
Before running the Example you need to have following files created as described.

1. HTMLStyleSheet.xsl (given below) as input XSLT.
2. SourceXML.xml (given below) as input XML.
3. Employees.html (an blank file - to avoid FileNotFoundException while transformation) as output file.

SourceXML.xml given below will be used as input XML. It will be transformed to well formed HTML Document after transforming.


SourceXML.xml
<?xml version="1.0"?>
<Employees>
 <Employee>
  <EmpId>1</EmpId>
  <Name>Kapil dev</Name>
  <Address1>Delhi</Address1>
  <Address2>India</Address2>
  <BirthDate>1954-05-13T00:00:00+05:30</BirthDate>
  <Salary>500</Salary>
 </Employee>
 <Employee>
  <EmpId>2</EmpId>
  <Name>Sunil Gavaskar</Name>
  <Address1>Mumbai</Address1>
  <Address2>India</Address2>
  <BirthDate>1945-07-10T00:00:00+05:30</BirthDate>
  <Salary>700</Salary>
 </Employee>
 <Employee>
  <EmpId>3</EmpId>
  <Name>Alen Border</Name>
  <Address1>Sydney</Address1>
  <Address2>Australia</Address2>
  <BirthDate>1950-09-25T00:00:00+05:30</BirthDate>
  <Salary>400</Salary>
 </Employee>
 <Employee>
  <EmpId>4</EmpId>
  <Name>Sachin Tendulkar</Name>
  <Address1>Mumbai</Address1>
  <Address2>India</Address2>
  <BirthDate>1970-02-06T00:00:00+05:30</BirthDate>
  <Salary>2000</Salary>
 </Employee>
 <Employee>
  <EmpId>5</EmpId>
  <Name>Ricky Ponting</Name>
  <Address1>Melbourne</Address1>
  <Address2>Australia</Address2>
  <BirthDate>1968-02-15T00:00:00+05:30</BirthDate>
  <Salary>1500</Salary>
 </Employee>
</Employees>

HTMLStyleSheet.xsl given below will be used as input XSLT. This Style Sheet will transform SourceXML.xml to well formed HTML Document.

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

 <xsl:template match="/">
  <HTML>
   <BODY>
    <h1>
     <U>List of Employees</U>
    </h1>
    <TABLE BORDER="1">
     <TBODY>
      <TR>
       <TH>EmpId</TH>
       <TH>Name</TH>
       <TH>Address1</TH>
       <TH>Address2</TH>
       <TH>BirthDate</TH>
       <TH>Salary</TH>
      </TR>
      <xsl:for-each select="Employees">
       <xsl:for-each select="Employee">
        <TR>
         <TD>
          <xsl:value-of select="EmpId" />
         </TD>
         <TD>
          <xsl:value-of select="Name" />
         </TD>
         <TD>
          <xsl:value-of select="Address1" />
         </TD>
         <TD>
          <xsl:value-of select="Address2" />
         </TD>
         <TD>
          <xsl:value-of select="BirthDate" />
         </TD>
         <TD>
          <xsl:value-of select="Salary" />
         </TD>
        </TR>
       </xsl:for-each>
      </xsl:for-each>
     </TBODY>
    </TABLE>
   </BODY>
  </HTML>
 </xsl:template>
</xsl:stylesheet>

XmlToHtmlTransformation class contains transform method will accept source xml, source style sheet and output file. It will transform given source xml using style sheet and store result in output file. output file is Employees.html in our case.

XmlToHtmlTransformation.java
import java.io.*;
import javax.xml.transform.*;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

/**
 *
 * @author Tejas Purohit
 */
public class XmlToHtmlTransformation {

 public boolean transform(String stylesheet, String sourceXML, String result) {

  try {

   // Creating Transformer with XSLT
   Source sourceXSLT = new StreamSource(new FileInputStream(stylesheet));
   TransformerFactory tFactory = TransformerFactory.newInstance();
   Transformer transformer = tFactory.newTransformer(sourceXSLT);

   // Creating StreamSource with sourceXML
   Source streamSource = new StreamSource(new FileInputStream(sourceXML));
   
   // Creating StreamResult with result
   Result streamResult = new StreamResult(new FileOutputStream(result)); 

   transformer.transform(streamSource, streamResult); // Transforming

  } catch (Exception exception) {

   exception.printStackTrace();

   return false;

  }

  return true;
 }

 public static void main(String[] args) {

  XmlToHtmlTransformation xhTransformation = new XmlToHtmlTransformation();

  String stylesheet = "HTMLStyleSheet.xsl";
  String sourceXML = "SourceXML.xml";
  String result = "Employees.html";

  if (xhTransformation.transform(stylesheet, sourceXML, result)) {
   System.out.println("Successful.");
  } else {
   System.out.println("Error.");
  }
 }
}

Transformation process will generate result HTML and store in Employees.html. Once Transformation is done Employees.html will look like below:

Employees.html
<HTML>
 <BODY>
  <h1>
   <U>List of Employees</U>
  </h1>
  <TABLE BORDER="1">
   <TBODY>
    <TR>
     <TH>EmpId</TH>
     <TH>Name</TH>
     <TH>Address1</TH>
     <TH>Address2</TH>
     <TH>BirthDate</TH>
     <TH>Salary</TH>
    </TR>
    <TR>
     <TD>1</TD>
     <TD>Kapil dev</TD>
     <TD>Delhi</TD>
     <TD>India</TD>
     <TD>1954-05-13T00:00:00+05:30</TD>
     <TD>500</TD>
    </TR>
    <TR>
     <TD>2</TD>
     <TD>Sunil Gavaskar</TD>
     <TD>Mumbai</TD>
     <TD>India</TD>
     <TD>1945-07-10T00:00:00+05:30</TD>
     <TD>700</TD>
    </TR>
    <TR>
     <TD>3</TD>
     <TD>Alen Border</TD>
     <TD>Sydney</TD>
     <TD>Australia</TD>
     <TD>1950-09-25T00:00:00+05:30</TD>
     <TD>400</TD>
    </TR>
    <TR>
     <TD>4</TD>
     <TD>Sachin Tendulkar</TD>
     <TD>Mumbai</TD>
     <TD>India</TD>
     <TD>1970-02-06T00:00:00+05:30</TD>
     <TD>2000</TD>
    </TR>
    <TR>
     <TD>5</TD>
     <TD>Ricky Ponting</TD>
     <TD>Melbourne</TD>
     <TD>Australia</TD>
     <TD>1968-02-15T00:00:00+05:30</TD>
     <TD>1500</TD>
    </TR>
   </TBODY>
  </TABLE>
 </BODY>
</HTML>

Which generates html output as below:

List of Employees

EmpIdNameAddress1Address2BirthDateSalary
1Kapil devDelhiIndia1954-05-13T00:00:00+05:30500
2Sunil GavaskarMumbaiIndia1945-07-10T00:00:00+05:30700
3Alen BorderSydneyAustralia1950-09-25T00:00:00+05:30400
4Sachin TendulkarMumbaiIndia1970-02-06T00:00:00+05:302000
5Ricky PontingMelbourneAustralia1968-02-15T00:00:00+05:301500

Comments