如何使用Java中的XSLT将XML转换为HTML [英] How to convert XML to HTML using XSLT in Java

查看:163
本文介绍了如何使用Java中的XSLT将XML转换为HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我希望借助XSLT将XML文件转换为HTML文件。但我收到一个错误,即

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:1字节UTF-8序列的
字节1无效。

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文件

<?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文件

<?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代码

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();
    }
}
}


推荐答案

对我来说,这看起来像编码问题。尝试确保在所有情况下都能正确编码文件。

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

例如,添加 encoding =UTF-8到您的XML和XSLT文件。但请注意,这只是一个声明 - 它不会更改字符本身。

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.

此外,您可以将XML内容复制到一个简单的编辑器中并将其明确保存为UTF -8。
例如,如果您使用的是Windows,请将内容复制到记事本中,点击另存为...。在文件对话框中,您可以从下拉列表中选择UTF-8。

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.

这篇关于如何使用Java中的XSLT将XML转换为HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆