title: How to convert XML to HTML using XSLT in Java
body: <p>I want to convert XML file into HTML file with help of XSLT. But I am getting an error i.e.</p>
  
  <blockquote>
    <p>javax.xml.transform.TransformerException:
    javax.xml.transform.TransformerException:
    com.sun.org.apache.xml.internal.utils.WrappedRuntimeException: Invalid
    byte 1 of 1-byte UTF-8 sequence.</p>
  </blockquote>
  
  <p>XML file</p>
  
  <pre><code>&lt;?xml version="1.0"?&gt;
  &lt;Company&gt;
      &lt;Info&gt;
              &lt;EmpId&gt;1&lt;/EmpId&gt;
              &lt;EmpName&gt;John&lt;/EmpName&gt;
              &lt;Age&gt;25&lt;/Age&gt;
            &lt;Salary&gt;20000&lt;/Salary&gt;
     &lt;/Info&gt;
      &lt;Info&gt;
              &lt;EmpId&gt;2&lt;/EmpId&gt;
              &lt;EmpName&gt;Tony&lt;/EmpName&gt;
              &lt;Age&gt;27&lt;/Age&gt;
              &lt;Salary&gt;23000&lt;/Salary&gt;
      &lt;/Info&gt;
      &lt;Info&gt;
              &lt;EmpId&gt;3&lt;/EmpId&gt;
              &lt;EmpName&gt;Eithen&lt;/EmpName&gt;
              &lt;Age&gt;29&lt;/Age&gt;
              &lt;Salary&gt;25000&lt;/Salary&gt;
      &lt;/Info&gt;
  &lt;/Company&gt;
  </code></pre>
  
  <p>XSL file</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0"&gt;
     &lt;xsl:template match="/"&gt;
        &lt;html&gt;
           &lt;body&gt;
              &lt;h1&gt;Company Details&lt;/h1&gt;
              &lt;table border="1"&gt;
                 &lt;tr&gt;
                    &lt;th&gt;EmpId&lt;/th&gt;
                    &lt;th&gt;EmpName&lt;/th&gt;
                    &lt;th&gt;Age&lt;/th&gt;
                    &lt;th&gt;Salary&lt;/th&gt;
                 &lt;/tr&gt;
                 &lt;xsl:for-each select="Company/Info"&gt;
                    &lt;tr&gt;
                       &lt;td&gt;
                          &lt;xsl:value-of select="EmpId" /&gt;
                       &lt;/td&gt;
                       &lt;td&gt;
                          &lt;xsl:value-of select="EmpName" /&gt;
                       &lt;/td&gt;
                       &lt;td&gt;
                          &lt;xsl:value-of select="Age" /&gt;
                       &lt;/td&gt;
                       &lt;td&gt;
                          &lt;xsl:value-of select="Salary" /&gt;
                       &lt;/td&gt;
                    &lt;/tr&gt;
                 &lt;/xsl:for-each&gt;
              &lt;/table&gt;
           &lt;/body&gt;
        &lt;/html&gt;
     &lt;/xsl:template&gt;
  &lt;/xsl:stylesheet&gt;
  </code></pre>
  
  <p>Java Code</p>
  
  <pre><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();
      }
  }
  }
  </code></pre>