【问题标题】:Class not Found exception for saxon parser implementation未找到撒克逊解析器实现的类异常
【发布时间】:2015-03-02 11:31:10
【问题描述】:

我正在使用“Saxon-HE 9.6.0-4”将 xml 数据转换为 HTML。 我收到以下错误。 java.lang.ClassNotFoundException: net.sf.saxon.TransformerFactoryImpl

[javax.xml.transform.Source xmlSource = 新 javax.xml.transform.stream.StreamSource(results.getDirectory() + "\results.xml"); javax.xml.transform.Source xsltSource = new javax.xml.transform.stream.StreamSource(xsltFile); StringWriter sw = new StringWriter();

    javax.xml.transform.Result result = new javax.xml.transform.stream.StreamResult(sw);
    System.setProperty("javax.xml.transform.TransformerFactory", "net.sf.saxon.TransformerFactoryImpl");
    TransformerFactory transFact = TransformerFactory.newInstance("net.sf.saxon.TransformerFactoryImpl", null);
    javax.xml.transform.Transformer trans = transFact.newTransformer(xmlSource);

    trans.transform(xsltSource, result);]

谁能帮我解决这个问题

【问题讨论】:

  • 确保在运行 Java 代码时 Saxon 位于类路径中。
  • 我有一个 pom 条目,它是一个 maven 项目。我在我的插件下创建了一个运行时文件夹,其中包含所有 jar 文件。在运行时这些 jars 应该被调用。我有一个到 maven 存储库的 java 类路径,其中下载了 Saxon 文件。当你说 Saxon 应该在 JAVA 类路径上时,你想让我在系统环境中设置它吗?
  • 我遇到了同样的问题,我正在使用 Maven 获取库。

标签: java xslt-2.0 saxon transformer


【解决方案1】:

错误

java.lang.ClassNotFoundException: net.sf.saxon.TransformerFactoryImpl

只有一种可能的含义:Saxon JAR 文件不在您的类路径中。

【讨论】:

  • 我有 xml 和 xslt 文件,我需要将 xml 文件转换为 html。使用 Saxson-HE 9。你能提供我的示例代码吗?
  • 不是代码问题,是配置问题。如果不彻底了解您运行应用程序的环境,我无法告诉您如何解决配置问题。
【解决方案2】:

下面是一个简单的例子
xml:

<?xml version="1.0" encoding="UTF-8"?>
<library>
    <books dept="physics">
        <book id="PHY00001" category="Atomic">
            <name>Concepts of Physics</name>
            <author>H.C.Verma</author>
            <isbn>8177091875</isbn>
            <price>15</price>
            <publisher>Tata</publisher>
        </book>
        <book id="PHY00002" category="Natural">
            <name>Handbook of Physics</name>
            <author>Nipendra Bhatnagar</author>
            <isbn>8177091876</isbn>
            <price>20</price>
            <publisher>Wiley</publisher>
        </book>
        <book id="PHY00003" category="Natural">
            <name>Handbook of Physics II</name>
            <author>Nipendra Bhatnagar</author>
            <isbn>8177091886</isbn>
            <price>25</price>
            <publisher>Wiley Publ.</publisher>
        </book>
    </books>
    <books dept="chemistry">
        <book id="CHE00001" category="Organic">
            <name>Chemistry Formulae And Definitions</name>
            <author>Ramanand Thakur</author>
            <isbn>8177091878</isbn>
            <price>20</price>
            <publisher>ChemWorld</publisher>
        </book>
        <book id="CHE00002" category="Inorganic">
            <name>Handbook of Chemistry</name>
            <author>Hansraj Modi</author>
            <isbn>8177091879</isbn>
            <price>35</price>
            <publisher>BetaBooks</publisher>
        </book>
        <book id="CHE00003" category="Inorganic">
            <name>Handbook of Chemistry II</name>
            <author>Hansraj Modi</author>
            <isbn>8177091889</isbn>
            <price>38</price>
            <publisher>Beta Marketing</publisher>
        </book>
    </books>
</library>

xsl:

<?xml version="1.0"?>
<xsl:stylesheet xmlns:xsl="http://www.w3.org/1999/XSL/Transform"
  version="1.0">
  <xsl:template match="/">
    <html>
      <head>
        <title>Books List</title>
      </head>
      <body>
        <table border="1">
          <tr>
            <th>Book Name</th>
            <th>Price</th>
          </tr>
          <xsl:for-each select="library/books/book">
            <tr>
              <td>
                <xsl:value-of select="name" />
              </td>
              <td>
                <xsl:value-of select="price" />
              </td>
            </tr>
          </xsl:for-each>
        </table>
      </body>
    </html>
  </xsl:template>
</xsl:stylesheet>

MainApp.java

import java.io.File;

import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerConfigurationException;
import javax.xml.transform.TransformerException;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource;

public class App {
    public static void main(String[] args) {
        System.setProperty("javax.xml.transform.TransformerFactory", "net.sf.saxon.TransformerFactoryImpl");
        try {
            transform("library.xml", "library.xsl");
        } catch (Exception ex) {
            System.out.println(ex.getMessage());
        }
    }

    public static void transform(String xml, String xsl)
            throws TransformerException, TransformerConfigurationException {
        TransformerFactory tfactory = TransformerFactory.newInstance();
        Transformer transformer = tfactory.newTransformer(new StreamSource(new File(xsl)));
        transformer.transform(new StreamSource(new File(xml)), new StreamResult(System.out));
    }
}

输出

<html>
   <head>
      <meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
      <title>Books List</title>
   </head>
   <body>
      <table border="1">
         <tr>
            <th>Book Name</th>
            <th>Price</th>
         </tr>
         <tr>
            <td>Concepts of Physics</td>
            <td>15</td>
         </tr>
         <tr>
            <td>Handbook of Physics</td>
            <td>20</td>
         </tr>
         <tr>
            <td>Handbook of Physics II</td>
            <td>25</td>
         </tr>
         <tr>
            <td>Chemistry Formulae And Definitions</td>
            <td>20</td>
         </tr>
         <tr>
            <td>Handbook of Chemistry</td>
            <td>35</td>
         </tr>
         <tr>
            <td>Handbook of Chemistry II</td>
            <td>38</td>
         </tr>
      </table>
   </body>
</html>

【讨论】:

    猜你喜欢
    • 2021-12-22
    • 1970-01-01
    • 1970-01-01
    • 2013-12-29
    • 2015-02-21
    • 2016-01-25
    • 2010-10-02
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多