I want to convert an XML file to an HTML file using XSLT. But I get an error message.
javax.xml.transform.TransformerException: javax.xml.transform.TransformerException: com.sun.org.apache.xml.internal.utils.WrappedRuntimeException: Invalid byte 1 of the 1-byte sequence of UTF-8.
Xml file
<?xml version="1.0"?> <Company> <Info> <EmpId>1</EmpId> <EmpName>John</EmpName> <Age>25</Age> <Salary>20000</Salary> </Info> <Info> <EmpId>2</EmpId> <EmpName>Tony</EmpName> <Age>27</Age> <Salary>23000</Salary> </Info> <Info> <EmpId>3</EmpId> <EmpName>Eithen</EmpName> <Age>29</Age> <Salary>25000</Salary> </Info> </Company>
XSL file
<?xml version="1.0" encoding="UTF-8"?> <xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"> <xsl:template match="/"> <html> <body> <h1>Company Details</h1> <table border="1"> <tr> <th>EmpId</th> <th>EmpName</th> <th>Age</th> <th>Salary</th> </tr> <xsl:for-each select="Company/Info"> <tr> <td> <xsl:value-of select="EmpId" /> </td> <td> <xsl:value-of select="EmpName" /> </td> <td> <xsl:value-of select="Age" /> </td> <td> <xsl:value-of select="Salary" /> </td> </tr> </xsl:for-each> </table> </body> </html> </xsl:template> </xsl:stylesheet>
Java code
public class TransInfoHtml { public static void main(String args[]) { try { TransformerFactory tFactory=TransformerFactory.newInstance(); Source xslDoc=new StreamSource("files/NewStylesheet.xsl"); Source xmlDoc=new StreamSource("D:/Demo.xml"); String outputFileName="CompanyInfo.html"; OutputStream htmlFile=new FileOutputStream(outputFileName); Transformer trasform=tFactory.newTransformer(xslDoc); trasform.transform(xmlDoc, new StreamResult(htmlFile)); } catch (FileNotFoundException e) { e.printStackTrace(); } catch (TransformerConfigurationException e) { e.printStackTrace(); } catch (TransformerFactoryConfigurationError e) { e.printStackTrace(); } catch (TransformerException e) { e.printStackTrace(); } } }
source share