当前位置: 技术问答>java相关
如何用JSP将XML和XSL生成HTML ?
来源: 互联网 发布时间:2015-05-14
本文导语: 现有XML文件及相应的XSL文件。 我的目的是将XML + XSL =( JSP )=〉HTML , 我利用JSP 及 apache的 xerces.jar , xalan.jar 的包 程序如下: Guide 可是出现如下错误: 500 Servlet Exception /jsp/guide.jsp:10: 找不到类 _jsp.XSLTPro...
现有XML文件及相应的XSL文件。
我的目的是将XML + XSL =( JSP )=〉HTML ,
我利用JSP 及 apache的 xerces.jar , xalan.jar 的包
程序如下:
Guide
可是出现如下错误:
500 Servlet Exception
/jsp/guide.jsp:10: 找不到类 _jsp.XSLTProcessor。
XSLTProcessor processor=XSLTProcessorFactory.getProcessor();
^
/jsp/guide.jsp:10: 未定义变量或类名:XSLTProcessorFactory
XSLTProcessor processor=XSLTProcessorFactory.getProcessor();
^
/jsp/guide.jsp:17: 异常 org.xml.sax.SAXException 永远不在相应 try 语句的主体中引发。
catch(SAXException e)
我用的平台是 win2000+apache+resin2.2
如果此方法不行,经介绍其他方法,请付jsp 例程
我的目的是将XML + XSL =( JSP )=〉HTML ,
我利用JSP 及 apache的 xerces.jar , xalan.jar 的包
程序如下:
Guide
可是出现如下错误:
500 Servlet Exception
/jsp/guide.jsp:10: 找不到类 _jsp.XSLTProcessor。
XSLTProcessor processor=XSLTProcessorFactory.getProcessor();
^
/jsp/guide.jsp:10: 未定义变量或类名:XSLTProcessorFactory
XSLTProcessor processor=XSLTProcessorFactory.getProcessor();
^
/jsp/guide.jsp:17: 异常 org.xml.sax.SAXException 永远不在相应 try 语句的主体中引发。
catch(SAXException e)
我用的平台是 win2000+apache+resin2.2
如果此方法不行,经介绍其他方法,请付jsp 例程
|
import javax.xml.transform.*;
javax.xml.transform.stream.*;
建议用jaxp接口
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer("http://localhost:8080/jsp/guide.xsl");
t.transform(new StreamSource("http://localhost:8080/jsp/xiaoyuanwenhua.xml"), new StreamResult(out));
javax.xml.transform.stream.*;
建议用jaxp接口
TransformerFactory tf = TransformerFactory.newInstance();
Transformer t = tf.newTransformer("http://localhost:8080/jsp/guide.xsl");
t.transform(new StreamSource("http://localhost:8080/jsp/xiaoyuanwenhua.xml"), new StreamResult(out));
|
我用的是apache的xalan-j,返回的String就是html:
public static String Transform(String xmlFileName, String xslFileName)
throws Exception
{
StringWriter writer = new StringWriter();
StreamResult streamResult = new StreamResult(writer);
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = null;
try
{
transformer = tFactory.newTransformer(new StreamSource(xslFileName));
}
catch(TransformerConfigurationException _ex)
{
}
File file = new File(xmlFileName);
try
{
transformer.transform(new StreamSource(file), streamResult);
}
catch(TransformerException _ex)
{
}
return ((StringWriter)streamResult.getWriter()).toString();
}
public static String Transform(String xmlFileName, String xslFileName)
throws Exception
{
StringWriter writer = new StringWriter();
StreamResult streamResult = new StreamResult(writer);
TransformerFactory tFactory = TransformerFactory.newInstance();
Transformer transformer = null;
try
{
transformer = tFactory.newTransformer(new StreamSource(xslFileName));
}
catch(TransformerConfigurationException _ex)
{
}
File file = new File(xmlFileName);
try
{
transformer.transform(new StreamSource(file), streamResult);
}
catch(TransformerException _ex)
{
}
return ((StringWriter)streamResult.getWriter()).toString();
}
|
如果存在apache的 xerces.jar , xalan.jar两个包的话,那就应该是路径的问题了
|
你吧那两个文件拷贝到jre/lib/ext下看看