您的位置:首页 > 其它

使用JDOM处理XML数据之PDF篇(二)

2008-04-25 13:44 741 查看
使用JDOM处理XML数据之PDF篇(二) XML文件的代码和XML:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" />http://www.csdn.net/Develop/read_article.asp?id=20733 相同格式转换的XSLT-FO文件的代码如下:watchlist.PDF.xsl<?XML version="1.0"?><xsl:stylesheet XMLns:xsl="http://www.w3.org/1999/XSL/Transform" version="1.0" XMLns:fo="http://www.w3.org/1999/XSL/Format"><xsl:template match ="/"> <fo:root XMLns:fo="http://www.w3.org/1999/XSL/Format"> <!-- defines page layout --> <fo:layout-master-set> <fo:simple-page-master master-name="simple" page-height="29.7cm" page-width="21cm" margin-top="1.5cm" margin-bottom="2cm" margin-left="2.5cm" margin-right="2.5cm"> <fo:region-body margin-top="3cm"/> <fo:region-before extent="1.5cm"/> <fo:region-after extent="1.5cm"/> </fo:simple-page-master> </fo:layout-master-set> <!-- defines the content --> <fo:page-sequence master-name="simple"> <fo:static-content flow-name="xsl-region-before"> <fo:block text-align="end" font-size="10pt" font-family="serif" line-height="14pt" > Watch List - Customer #<xsl:value-of select="./quote-list/customer/@id"/> </fo:block> </fo:static-content> <fo:flow flow-name="xsl-region-body"> <fo:block font-size="16pt" font-family="sans-serif" font-weight="bold" line-height="26pt" space-after.optimum="12pt" background-color="blue" color="white" text-align="center"> Your Stock Watch List </fo:block> <fo:block font-size="12pt" font-family="sans-serif" font-weight="bold" line-height="18pt" space-after.optimum="10pt" start-indent="10pt"> Hello, <xsl:value-of select="./quote-list/customer/@first-name"/> </fo:block> <fo:block font-size="10pt" font-family="sans-serif" font-style="italic" line-height="18pt" space-after.optimum="10pt" start-indent="15pt"> Prices were obtained at <xsl:value-of select="./quote-list/@time"/> on <xsl:value-of select="./quote-list/@date"/> </fo:block> <fo:table> <fo:table-column column-width="3cm"/> <fo:table-column column-width="7cm"/> <fo:table-column column-width="3cm"/> <fo:table-header font-size="10pt" line-height="14pt" font-family="sans-serif"> <fo:table-row font-weight="bold"> <fo:table-cell text-align="start"> <fo:block>SYMBOL</fo:block> </fo:table-cell> <fo:table-cell text-align="start"> <fo:block>COMPANY NAME</fo:block> </fo:table-cell> <fo:table-cell text-align="start"> <fo:block>SHARE PRICE</fo:block> </fo:table-cell> </fo:table-row> </fo:table-header> <fo:table-body font-size="10pt" line-height="16pt" font-family="sans-serif"> <xsl:for-each select="//quote"> <fo:table-row> <fo:table-cell> <fo:block text-align="start" > <xsl:value-of select="@symbol"/> </fo:block> </fo:table-cell> <fo:table-cell> <fo:block text-align="start" > <xsl:value-of select="@name"/> </fo:block> </fo:table-cell> <fo:table-cell> <fo:block text-align="start" > $ <xsl:value-of select="./price[@currency='USD']/@amount"/> </fo:block> </fo:table-cell> </fo:table-row> </xsl:for-each> </fo:table-body> </fo:table> </fo:flow> </fo:page-sequence> </fo:root></xsl:template></xsl:stylesheet>最后我们在一个JSP文件中来调用这个javabean:PDFWriter.jsp<%@ page contentType="text/html; charset=gb2312" %><%@ page import="org.JDOM.*" %><%@ page import="org.JDOM.output.*" %><%@ page import="org.JDOM.input.*" %><%@ page import="java.io.*" %><%@ page import="java.util.*" %><%@ page import="XML.*" %><% String aa=getServletContext().getRealPath("/")+"j2ee_XML/"; String fileName="watchlist.XML"; String trace=aa+fileName; String styleName="watchlist.PDF.xsl"; String styleTrace=aa+styleName; String resultName="aa.PDF"; String resultTrace=aa+resultName; XML.PDFWriter PDFWriter = new XML.PDFWriter(); try { PDFWriter.createPDFFromXML(styleTrace,trace, resultTrace); } catch (Exception e) { System.out.println(e.getMessage()); }%>aa.PDF就是格式化后的结果。到现在使用XML数据的三种方式都介绍过了:直接读取:http://www.csdn.net/Develop/read_article.asp?id=20720使用XSLT转化成HTML:http://www.csdn.net/Develop/read_article.asp?id=20733和这篇里介绍的使用XSL-FO转化成二进制格式。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: