XSL解析xml

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页面上:

编号 名称 数量 价格(元) 出版时间
1A1015.202010-05-06
2B2035.502010-08-01
3C3032.002010-08-02
4D4065.802010-08-06

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
©️2022 CSDN 皮肤主题:精致技术 设计师:CSDN官方博客 返回首页
评论
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值