2015-03-02 52 views
0

我正在使用「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() +「\結果。 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);] 

可有人請幫我出這個問題

+0

確保撒克遜是在類路徑當您運行Java代碼。 – 2015-03-02 12:38:48

+0

我有一個pom條目,它是一個maven項目。我在我的插件下創建了一個包含所有jar文件的運行時文件夾。運行期間應該調用這些罐子。我有一個java類路徑到Maven倉庫,下載Saxon文件。當你說Saxon應該在JAVA類路徑上時,你希望我在系統環境中設置它? – user1502919 2015-03-03 12:09:57

+0

我遇到同樣的問題,我正在使用Maven來獲取庫。 – kevingreen 2015-06-19 20:35:11

回答

1

錯誤

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

只有一個可能的含義:撒克遜人的JAR文件不在類路徑中。

+0

我有xml和xslt文件需要將xml文件轉換爲html。使用Saxson-HE 9.你能給我提供示例代碼嗎? – user1502919 2015-03-10 11:08:47

+0

這不是一個代碼問題,這是一個配置問題。如果不徹底瞭解運行應用程序的環境,我無法告訴您如何解決配置問題。 – 2015-03-10 17:05:23

0

下面
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)); 
    } 
} 

Ouptut

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