用Transformer在服务端就转成html文件,
这个论坛里有很多例子。
这个论坛里有很多例子。
解决方案 »
- 求ssh2框架web开发的HR权限管理解决方案!
- jsp中视频播放器代码播放avi格式的有声音没有图像
- 如何配置ehcache 缓存指定的请求或jsp
- java.lang.NoClassDefFoundError 问题解决
- 对外数据接口的安全认证解决方案?
- 帮忙写个SQL语句(在线等)
- 通过传递参数在页面上显示内容
- Hibernate数据类型问题,求教!
- JSP中的Date问题
- 上线项目报错Could not get a databaseId from dataSource
- sos.java泛型使用为什么这个错误..谁能帮我
- ■■困惑,使用JBuilder按理CMP EntityBean。■■
import javax.xml.transform.stream.StreamResult;
import javax.xml.transform.stream.StreamSource; /**
* 这个方法将xml通过样式单转换.
* @param xmlfile 将要被转换的xml文件路径
* @param xslfile XSL文件的文件路径
* @return String 转换后的字符串形式存放的html
*/
public static String ExecuteXSL(String xmlfile, String xslfile) {
try {
ByteArrayOutputStream byteRep = new ByteArrayOutputStream();
TransformerFactory transformerFactory =
TransformerFactory.newInstance();
StreamSource source = new StreamSource(xmlfile);
StreamResult result = new StreamResult(byteRep);
StreamSource style = new StreamSource(xslfile);
Transformer transformer =
transformerFactory.newTransformer(style);
transformer.setOutputProperty(javax.xml.transform.OutputKeys.ENCODING,
"GB2312"); //\u8BBE\u7F6E\u7F16\u7801
transformer.transform(source, result);
return byteRep.toString();
}
catch (Exception e) {
e.printStackTrace();
return null;
} }
我的xsl如下:
<xsl:stylesheet xmlns:xsl="http://www.w3.org/TR/WD-xsl">
<xsl:template match="/">
<HTML>
<HEAD>
<TITLE><xsl:value-of select="document/title"/></TITLE>
</HEAD>
<BODY>
<H1><xsl:value-of select="document/title"/></H1>
<xsl:apply-templates select="document/section"/>
</BODY>
</HTML>
</xsl:template>
<xsl:template match="section">
<DIV>
<H2><xsl:value-of select="title"/></H2>
<xsl:apply-templates />
</DIV>
</xsl:template> <xsl:template match="p">
<P><xsl:apply-templates /></P>
</xsl:template> <xsl:template match="list">
<UL>
<xsl:for-each select="item">
<LI><xsl:apply-templates /></LI>
</xsl:for-each>
</UL>
</xsl:template> <xsl:template match="emph">
<I><xsl:apply-templates /></I>
</xsl:template> <xsl:template match="text()"><xsl:value-of /></xsl:template>
</xsl:stylesheet>
xml如下:
<?xml version="1.0"?>
<?xml-stylesheet type="text/xsl" href="pole.xsl"?>
<document>
<title>To the Pole and Back</title>
<section>
<title>The First Day</title>
<p>It was the <emph>best</emph> of days, it was the
<emph>worst</emph> of days.</p>
<list>
<item><emph>best</emph> in that the sun was out.</item>
<item><emph>worst</emph> in that it was 39 degrees below zero.</item>
</list>
<section>
<title>Lunch Menu</title>
<list>
<item>ice cream</item>
<item>popsicles</item>
</list>
</section> </section> <section>
<title>The Second Day</title>
<p>Ditto the first day.</p>
</section></document>
楼上的兄弟可以试试