1.解析步骤
(1)取得xml文件的内容xmlContent(通过IO流或者由webservice得到应答字符串)
(2)根据xmlContent构造输入流xmlStream
InputStream xmlStream = new ByteArrayInputStream(xmlContent.getBytes("UTF-8");
(3)根据xsl文件的路径构造输入流xsltStream
InputStream xsltStream = new FileInputStream(xslFilePath);
(4)由xsltStream构造Source 对象xsltSource
Source xsltSource = new StreamSource(xsltStream);
(5)取得TransformerFactory的实例factory
TransformerFactory factory = TransformerFactory.newInstance();
(6)根据xsltSource,从factory 中构造Transformer 对象trans
Transformer trans = factory.newTransformer(xsltSource);
(7) 给xsl文件中传递参数(不是必须的)
trans.setParameter("参数名", 参数值);
trans.setOutputProperty(OutputKeys.ENCODING, "gbk");//加入这句可防止中文乱码
(8)开始xsl解析
result = new StreamResult(new ByteArrayOutputStream());
trans.transform(new StreamSource(xmlStream), result);
(9)得到xsl解析之后的字符串
result.getOutputStream().toString()
2.具体实例
(1)books.xml
<?xml version="1.0" encoding="utf-8"?> <books password="123"> <book kinds="A"> <id>1</id> <name>A</name> <number>10</number> <price>1520</price> <printdate>20100506</printdate> <seconds>259200</seconds> </book> <book kinds="A"> <id>2</id> <name>B</name> <number>20</number> <price>3550</price> <printdate>20100801</printdate> <seconds>86400</seconds> </book> <book kinds="A"> <id>3</id> <name>C</name> <number>30</number> <price>3200</price> <printdate>20100802</printdate> <seconds>43200</seconds> </book> <book kinds="A"> <id>4</id> <name>D</name> <number>40</number> <price>6580</price> <printdate>20100806</printdate> <seconds>129600</seconds> </book> <book kinds="unknown"> <id>4</id> <name>D</name> <number>40</number> <price>6580</price> <printdate>20100806</printdate> <seconds>129600</seconds> </book> </books>
(2)books.xsl<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform"> <xsl:output omit-xml-declaration="yes" encoding="UTF-8" method="html" /> <!-- 小数位数为2(分转换成元) --> <xsl:param name="DecimalLen"> 2 </xsl:param> <xsl:param name="pwd" /> <!-- 小数点 --> <xsl:param name="PointSplit"> <xsl:value-of select="'.'" /> </xsl:param> <!-- 日期格式 dd.MM.yyyy --> <xsl:param name="DateSplit"> <xsl:value-of select="'.'" /> </xsl:param> <!-- 解析开始入口 --> <xsl:template match="books"> <!--解析凭证 xml文件中的password必须等于程序中传过来的参数 --> <xsl:if test="@password = $pwd"> <table> <thead> <tr> <th align="middle"> 编号 </th> <th align="middle"> 名称 </th> <th align="middle"> 数量 </th> <th align="middle"> 价格(元) </th> <th align="middle"> 出版时间 </th> </tr> </thead> <tbody> <xsl:for-each select="./book"> <xsl:if test="@kinds='A'"> <tr> <td> <xsl:value-of select="./id" /> </td> <td> <xsl:value-of select="./name" /> </td> <td> <xsl:value-of select="./number" /> </td> <td> <!-- 将分转化成元输出 --> <xsl:call-template name="changeToYuan"> <xsl:with-param name="SrcNum" select="./price" /> <xsl:with-param name="LenNum" select="string-length(./price)" /> </xsl:call-template> </td> <td> <!-- 日期装化成"yyyy-MM-dd" --> <xsl:call-template name="changeToDate"> <xsl:with-param name="date" select="./printdate" /> </xsl:call-template> </td> </tr> </xsl:if> </xsl:for-each> </tbody> </table> </xsl:if> </xsl:template> <!-- xml文件中的对应的金额都是分,需要转换元(除以100) --> <xsl:template name="changeToYuan"> <xsl:param name="SrcNum" /> <xsl:param name="LenNum" /> <xsl:choose> <xsl:when test="$LenNum=1"> <xsl:value-of select="'0.0'" /> <xsl:value-of select="$SrcNum" /> </xsl:when> <xsl:when test="$LenNum=2"> <xsl:value-of select="'0.'" /> <xsl:value-of select="$SrcNum" /> </xsl:when> <xsl:when test="$LenNum > 2"> <xsl:value-of select="substring($SrcNum, 1, ($LenNum - $DecimalLen))" /> <xsl:value-of select="$PointSplit" /> <xsl:value-of select="substring($SrcNum, ($LenNum - $DecimalLen + 1), $DecimalLen)" /> </xsl:when> </xsl:choose> </xsl:template> <!--日期装化成"yyyy-MM-dd"--> <xsl:template name="changeToDate"> <xsl:param name="date" /> <xsl:value-of select="substring($date,1,4)" /> - <xsl:value-of select="substring($date,5,2)" /> - <xsl:value-of select="substring($date,7,2)" /> </xsl:template> </xsl:stylesheet>
(3)XSLPractice.java
import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; import java.io.File; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.io.UnsupportedEncodingException; import javax.xml.transform.Source; import javax.xml.transform.Transformer; import javax.xml.transform.TransformerConfigurationException; import javax.xml.transform.TransformerException; import javax.xml.transform.TransformerFactory; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; public class XSLPractice { /** * @param args */ public static void main(String[] args) { // TODO Auto-generated method stub System.out.println(getHtmlString("src/books.xml","src/books.xsl","123")); } private static String getHtmlString(String xmlFilePath,String xslFilePath,String password){ StreamResult result = null; String xmlContent=""; try { //通过流读取xml文件的内容 InputStreamReader fs= new InputStreamReader(new FileInputStream(new File(xmlFilePath)),"utf-8"); int i=0; int b=0; while((b=fs.read())!=-1){ xmlContent+=String.valueOf((char)b); } InputStream xmlStream = new ByteArrayInputStream(xmlContent.getBytes("UTF-8")); InputStream xsltStream = new FileInputStream(xslFilePath); Source xsltSource = new StreamSource(xsltStream); TransformerFactory factory = TransformerFactory.newInstance(); Transformer trans = factory.newTransformer(xsltSource); //给xsl文件中传递参数 trans.setParameter("pwd", password); result = new StreamResult(new ByteArrayOutputStream()); trans.transform(new StreamSource(xmlStream), result); }catch (FileNotFoundException e){ e.printStackTrace(); }catch (TransformerConfigurationException e){ e.printStackTrace(); }catch (TransformerException e) { e.printStackTrace(); }catch (Exception e){ e.printStackTrace(); } if (result == null || result.getOutputStream() == null){ return ""; } //返回xsl解析之后的字符串 return result.getOutputStream().toString(); } }
(4)将解析之后的字符串显示到jsp页面上:
编号 | 名称 | 数量 | 价格(元) | 出版时间 |
---|---|---|---|---|
1 | A | 10 | 15.20 | 2010-05-06 |
2 | B | 20 | 35.50 | 2010-08-01 |
3 | C | 30 | 32.00 | 2010-08-02 |
4 | D | 40 | 65.80 | 2010-08-06 |