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>
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>