How to convert XML to HTML using XSLT in Java
Asked Answered
D

3

7

I want to convert XML file into HTML file with help of XSLT. But I am getting an error i.e.

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.

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();
    }
}
}
Dionysius answered 6/11, 2013 at 11:59 Comment(0)
D
1

To me, this looks like an encoding problem. Try to ensure that the files are encoded correctly in all cases.

For example, add encoding="UTF-8" to your XML and XSLT file. But note that this is only a declaration - it does not change the characters themselves.

Also, you could copy your XML content into a simple editor and save it explicitly as UTF-8. For instance, if you are using windows, copy the content into notepad, hit "Save as...". In the file dialog, you can choose "UTF-8" from a drop-down.

Depriest answered 6/11, 2013 at 13:7 Comment(0)
H
0

Your code works fine. In the question stylesheet node was not closed.

<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
   <xsl:template match="/">
    ---
   </xsl:template>
</xsl:stylesheet>

Also check the xsl path properly or test by

Source xslDoc=new StreamSource("D:/NewStylesheet.xsl");`
Henriquez answered 6/11, 2013 at 12:12 Comment(1)
UTF-8 error is resolved. Now it's showing ERROR: 'com.sun.org.apache.xml.internal.utils.WrappedRuntimeException: The entity name must immediately follow the '&' in the entity reference. I checked XML file. It doesn't contain special characters and hierarchy of all tags is proper.plese helpDionysius
H
0

For the special case of convert XHTML pages to HTML, we can use the HTMLWriter from the dom4j API.

@Test
public void givenXHTML_whenWrite_thenGetHTML() throws Exception {
    String xml = "<?xml version='1.0' encoding='UTF-8' ?>" +
      "<html> <body><![CDATA[First&nbsp;test]]> " +
      "<img alt=\"W3C\" height=\"48\" width=\"72\" src=\"http://www.w3.org/Icons/w3c_home\" />" +
      "</body> </html>";
    Document document = DocumentHelper.parseText(xml);
    StringWriter buffer = new StringWriter();
    HTMLWriter writer = new HTMLWriter(buffer);
    String expects = "\n<html>\n" +
      "  <body>First&nbsp;test\n    "
      + "<img alt=\"W3C\" height=\"48\" width=\"72\" src=\"http://www.w3.org/Icons/w3c_home\">\n  "
      + "</body>\n" + "</html>\n";

    writer.write(document);
    String output = buffer.toString();

    assertThat(output).isEqualTo(expects);
}
Hallee answered 30/7, 2019 at 14:27 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.