我使用“Saxon-HE 9.6.0-4”将xml数据转换为HTML。 我收到以下错误。 java.lang.ClassNotFoundException:net.sf.saxon.TransformerFactoryImpl
[javax.xml.transform.Source xmlSource = new javax.xml.transform.stream.StreamSource(results.getDirectory() +“\ _结果”); 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 :(得分:1)
错误
java.lang.ClassNotFoundException: net.sf.saxon.TransformerFactoryImpl
只有一个可能的含义:Saxon JAR文件不在您的类路径中。
答案 1 :(得分: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>