现在有一个cdcatalog.xml文件,
<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Fmpire Burlesque</title>
<artist>FBob Dylan</artist>
<country>FUSA</country>
<company>FColumbia</company>
<price>20.90</price>
<year>2005</year>
</cd>
</catalog>
和一个cdcatalog.xsl文件,内容如下:
<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th align="left">Title</th>
<th align="left">Artist</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template></xsl:stylesheet>想把转换后的结果输出到result.xml文件中,
请问java代码应该怎么写?
<?xml version="1.0" encoding="ISO-8859-1"?>
<catalog>
<cd>
<title>Empire Burlesque</title>
<artist>Bob Dylan</artist>
<country>USA</country>
<company>Columbia</company>
<price>10.90</price>
<year>1985</year>
</cd>
<cd>
<title>Fmpire Burlesque</title>
<artist>FBob Dylan</artist>
<country>FUSA</country>
<company>FColumbia</company>
<price>20.90</price>
<year>2005</year>
</cd>
</catalog>
和一个cdcatalog.xsl文件,内容如下:
<?xml version="1.0" encoding="ISO-8859-1"?><xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"><xsl:template match="/">
<html>
<body>
<h2>My CD Collection</h2>
<table border="1">
<tr bgcolor="#9acd32">
<th align="left">Title</th>
<th align="left">Artist</th>
</tr>
<xsl:for-each select="catalog/cd">
<tr>
<td><xsl:value-of select="title"/></td>
<td><xsl:value-of select="artist"/></td>
</tr>
</xsl:for-each>
</table>
</body>
</html>
</xsl:template></xsl:stylesheet>想把转换后的结果输出到result.xml文件中,
请问java代码应该怎么写?
解决方案 »
- jsp 标签问题
- 商品种类,每个种类都有不同的属性的建模问题
- tomcat因服务性问题停止(2003下,tomcat启动不了)
- 求最新能用的eclipse,jboss,lomboz版本及下载地址.最好是直接的软件下载地址
- 关于struts验证validator的问题,请指教
- 新手操作數據庫問題
- ■■struts对多表多记录的FormBean应当如何设计呢,请给意见吧?■■
- 为什么绘制jpeg图的jsp代码,在tomcat行,jrun不行?
- 救命啊!jb8+wl7开发cmp时有关finder方法报错的问题
- spring事务不起作用
- 《请问csdn论坛一些东东西西~~~》
- spring mvc 怎么在后台接收到页面上传来的值
public static void main(String[] args) {
String xmlFileName = "d:/cdcatalog.xml";
String xslFileName = "d:/cdcatalog.xsl";
String htmlFileName = "d:/out.xml";
transform(xmlFileName, xslFileName, htmlFileName);
} public static void transform(String xmlFileName, String xslFileName,
String htmlFileName) {
try {
TransformerFactory tFac = TransformerFactory.newInstance();
Source xslSource = new StreamSource(xslFileName);
Transformer t = tFac.newTransformer(xslSource);
File xmlFile = new File(xmlFileName);
File htmlFile = new File(htmlFileName);
Source source = new StreamSource(xmlFile);
Result result = new StreamResult(htmlFile);
t.transform(source, result);
} catch (TransformerConfigurationException e) {
e.printStackTrace();
} catch (TransformerException e) {
e.printStackTrace();
}
}
这样可以。