xml to html or xml covert using xslt templates



Xml to html convert using xslt

<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:output indent="yes" method="html"/> <xsl:template match="/"> <html> <body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE"> <xsl:for-each select="breakfast_menu/food"> <div style="background-color:teal;color:white;padding:4px"> <span style="font-weight:bold"><xsl:value-of select="name"/> - </span> <xsl:value-of select="price"/> </div> <div style="margin-left:20px;margin-bottom:1em;font-size:10pt"> <p> <xsl:value-of select="description"/> <span style="font-style:italic"> (<xsl:value-of select="calories"/> calories per serving)</span> </p> </div> </xsl:for-each> </body> </html> </xsl:template> </xsl:stylesheet>


    <?xml version="1.0" encoding="UTF-8"?> <breakfast_menu> <food> <name>Belgian Waffles</name> <price>$5.95</price> <description>Two of our famous Belgian Waffles with plenty of real maple syrup</description> <calories>650</calories> </food> <food> <name>Strawberry Belgian Waffles</name> <price>$7.95</price> <description>Light Belgian waffles covered with strawberries and whipped cream</description> <calories>900</calories> </food> <food> <name>Berry-Berry Belgian Waffles</name> <price>$8.95</price> <description>Light Belgian waffles covered with an assortment of fresh berries and whipped cream</description> <calories>900</calories> </food> <food> <name>French Toast</name> <price>$4.50</price> <description>Thick slices made from our homemade sourdough bread</description> <calories>600</calories> </food> <food> <name>Homestyle Breakfast</name> <price>$6.95</price> <description>Two eggs, bacon or sausage, toast, and our ever-popular hash browns</description> <calories>950</calories> </food> </breakfast_menu>
    package com.kartik.xml.to.xml; import java.io.File; import java.io.FileInputStream; import java.io.InputStream; import java.io.StringReader; import java.io.StringWriter; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.transform.Templates; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.dom.DOMSource; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; public class XmlToXmlConvert { public static void main(String[] args) { String dataXML = convertXMLFileToString("C:\\Users\\kmandal\\Desktop\\breakFirst.xml"); Templates input =createTemplate(); XmlToXmlConvert st = new XmlToXmlConvert(); System.out.println(st.transform(dataXML, input)); /* String inputXSL = convertXMLFileToString("C:\\Users\\kmandal\\Desktop\\breakFirst.xslt"); String outputHTML = "C:\\Users\\kmandal\\Desktop\\break.html"; try{ st.transform(dataXML, inputXSL, outputHTML); }catch (TransformerConfigurationException e){ System.err.println("TransformerConfigurationException"); // System.err.println(e); }catch (TransformerException e){ System.err.println("TransformerException"); // System.err.println(e); }*/ } public void transform(String dataXML, String inputXSL, String outputHTML)throws TransformerConfigurationException,TransformerException { TransformerFactory factory = TransformerFactory.newInstance(); StreamSource xslStream = new StreamSource(inputXSL); Transformer transformer = factory.newTransformer(xslStream); StreamSource in = new StreamSource(dataXML); StreamResult out = new StreamResult(outputHTML); transformer.transform(in, out); System.out.println("The generated HTML file is:" + outputHTML); } /** * Transform xml using pfile to String * @param xmlDataInput * @param xsltTemplate * @return */ public String transform(String xmlDataInput, Templates xsltTemplate) { if ( xmlDataInput == null || xmlDataInput.trim().length() == 0 || xsltTemplate == null) { return null; } StringWriter stringWriter = new StringWriter(); try { Transformer transformer = xsltTemplate.newTransformer(); StreamSource source = new StreamSource(new StringReader(xmlDataInput)); javax.xml.transform.stream.StreamResult result = new javax.xml.transform.stream.StreamResult( stringWriter); transformer.transform(source, result); } catch (TransformerConfigurationException tce) { throw new RuntimeException("Error in XSLT rule transformation"); } catch (TransformerException te) { throw new RuntimeException("Error in XSLT rule transformation"); } return stringWriter.getBuffer().toString(); } /** * * @return */ public static Templates createTemplate(){ TransformerFactory tFactory = TransformerFactory.newInstance(); // String xslt = getXslt();//this is read from properties file String xslt = convertXMLFileToString("C:\\Users\\kmandal\\Desktop\\breakFirst.xslt"); if ( xslt != null ) { StreamSource stylesource = new StreamSource(new StringReader(xslt)); try { return tFactory.newTemplates(stylesource); } catch (TransformerConfigurationException e) { System.out.println("Error"); } } return null; } /** * * @param fileName * @return */ public static String convertXMLFileToString(String fileName) { try{ DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); InputStream inputStream = new FileInputStream(new File(fileName)); org.w3c.dom.Document doc = documentBuilderFactory.newDocumentBuilder().parse(inputStream); StringWriter stw = new StringWriter(); Transformer serializer = TransformerFactory.newInstance().newTransformer(); serializer.transform(new DOMSource(doc), new StreamResult(stw)); return stw.toString(); } catch (Exception e) { e.printStackTrace(); } return null; } }
OutPut:

    <html> <body style="font-family:Arial;font-size:12pt;background-color:#EEEEEE"> <div style="background-color:teal;color:white;padding:4px"> <span style="font-weight:bold">Belgian Waffles - </span>$5.95</div> <div style="margin-left:20px;margin-bottom:1em;font-size:10pt"> <p>Two of our famous Belgian Waffles with plenty of real maple syrup<span style="font-style:italic"> (650 calories per serving)</span> </p> </div> <div style="background-color:teal;color:white;padding:4px"> <span style="font-weight:bold">Strawberry Belgian Waffles - </span>$7.95</div> <div style="margin-left:20px;margin-bottom:1em;font-size:10pt"> <p>Light Belgian waffles covered with strawberries and whipped cream<span style="font-style:italic"> (900 calories per serving)</span> </p> </div> <div style="background-color:teal;color:white;padding:4px"> <span style="font-weight:bold">Berry-Berry Belgian Waffles - </span>$8.95</div> <div style="margin-left:20px;margin-bottom:1em;font-size:10pt"> <p>Light Belgian waffles covered with an assortment of fresh berries and whipped cream<span style="font-style:italic"> (900 calories per serving)</span> </p> </div> <div style="background-color:teal;color:white;padding:4px"> <span style="font-weight:bold">French Toast - </span>$4.50</div> <div style="margin-left:20px;margin-bottom:1em;font-size:10pt"> <p>Thick slices made from our homemade sourdough bread<span style="font-style:italic"> (600 calories per serving)</span> </p> </div> <div style="background-color:teal;color:white;padding:4px"> <span style="font-weight:bold">Homestyle Breakfast - </span>$6.95</div> <div style="margin-left:20px;margin-bottom:1em;font-size:10pt"> <p>Two eggs, bacon or sausage, toast, and our ever-popular hash browns<span style="font-style:italic"> (950 calories per serving)</span> </p> </div> </body> </html>

Previous
Next Post »