xml To html

import java.io.*;
import java.util.Date;

import org.jdom.Document;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import org.jdom.transform.XSLTransformer;


public class Test {

/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
transToHtml("F:\\test\\HelloWorld.xml","F:\\test\\HelloWorld.xsl");
}



/**
* 将xml文档输出到标准输出设备(流)并以指定的xsl样式.生成html
* @param xmlPath 指定的xml文档路径
* @param xslPath xsl文档路径
* @param 输出流
*/
public static void transformXSL(String xmlPath,String xslPath){
File fXml=new File(xmlPath);
File fXsl=new File(xslPath);
SAXBuilder saxb=new SAXBuilder();
XMLOutputter op=new XMLOutputter();

try {
XSLTransformer xslt=new XSLTransformer(fXsl);
Document xmldoc=saxb.build(fXml);
Format format=Format.getCompactFormat().setEncoding("gb2312");
op.setFormat(format);
op.output(xslt.transform(xmldoc), System.out);

} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}


/***
* 将xml文件按照指定格式转换html样式。
* @param xmlPath
* @param xstlPath
*/
public static void transToHtml(String xmlPath,String xstlPath){

//create a builder
SAXBuilder builder = new SAXBuilder();
//get a document from given
try {
Document doc = builder.build(new File(xmlPath));
//create a XSLTransformer from given
XSLTransformer xstl = new XSLTransformer(new File(xstlPath));
//get another document from given
Document doc2 = xstl.transform(doc);
//create an XMLOutputter
XMLOutputter outp = new XMLOutputter(Format.getPrettyFormat().setEncoding("UTF-8"));
//print the Document
outp.output(doc2, System.out);
} catch (JDOMException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}


}

}


xml格式
<?xml version="1.0" encoding="UTF-8"?>
<HelloWorld>
<sayHelloWorld author="wjm">
<name>wujinmiao</name>
<age>25</age>
</sayHelloWorld>
<sayHelloWorld author="wjc">
<name>wujincheng</name>
<age>23</age>
</sayHelloWorld>
<sayHelloWorld author="wjl">
<name>wujinling</name>
<age>22</age>
</sayHelloWorld>
</HelloWorld>
xstl:
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="HelloWorld">
<html>
<head>
<title>HelloWorld</title>
</head>
<body>
<table>
<tbody>
<tr>
<xsl:for-each select="/HelloWorld/sayHelloWorld">
<xsl:choose>
<xsl:when test="age >23">
<td bgcolor="#ff00ff">
<xsl:value-of select="name"/>
is 25
</td>
</xsl:when>
<xsl:when test="age=23">
<td bgcolor="#cccccc">
<xsl:value-of select="name"/>
is 23
</td>
</xsl:when>
<xsl:otherwise>
<td bgcolor="#cccccc">
wujinling
<xsl:value-of select="age"/>
</td>
</xsl:otherwise>
</xsl:choose>
</xsl:for-each>
</tr>
</tbody>
</table>

</body>
</html>
</xsl:template>
<xsl:template match="sayHelloWorld">
<xsl:value-of select="."/>
<br/>
</xsl:template>
</xsl:stylesheet>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值