【程序编程相关:ASP.NET中DataGrid鼠标经过】
【推荐阅读:如何防止用户重复提交数据】
【扩展信息:想起了PETER MURPHY】author: holyfaire-mail: holyfair@sina.com一. 序
在一些运用中,我们通常会把一些文本与配置信息转换成xml文件进行传输,修改,保存.特别是具有一定模板性质的文档用xml文件来实现其管理就显得相当的方便了.提供对于xml文件的操作的java api很多,诸于dom,jdom,castor,sax,xmlreader,xpath,xslt等等. 具体的这些api的用法这里就不多提了. 当使用这些接口实现xml的操作后,对于有些文档而言最终必须呈现给用户看的还是我们通常所熟悉的word与pdf文档.我们这里就来看一下从一个xml文件到rtf与pdf文件转换的实现.
二. 从xml到pdf
对于一个具有一定模板性质的xml文件,我们可以用fop api来实现其到pdf的转换.
fop需要fop.jar. 我们可以到http://xml.apache.org/fop/ 上获取与了解其用法. 以一个一般复杂的xml文件为例: 要转换xml文档 test.xml 如下:<featuresrs title="srs">
<introduction> <objective>objective here</objective> <scope>scope here</scope> <responsibilities>responsibilities here</responsibilities> <references>reference here</references> <daa> <term> term here </term> <definition> definition here </definition> </daa> </introduction> <generaldescription> <featurename> <summary>summary here</summary> <breakdown>breakdown here</breakdown> </featurename> <requirement> <content> content here. </content> </requirement> <requirement> <content> content2 here. </content> </requirement> <featureinteractions>featureinteractions here</featureinteractions> </generaldescription> <strresources> <strresource> <estring> estring here </estring> <resourceid> resourceid here </resourceid> <rqmt> rqmt here. </rqmt> </strresource> </strresources> </featuresrs> 对于这样一个xml文档,我们要将其转化成pdf格式必须建立一个xsl-fo文件,来定义对各element与value格 式的转换. 我们建立xsl-fo文件 test.xsl 如下: <?xml version="1.0" encoding="utf-8"?> <xsl:stylesheet version="1.1" xmlns:xsl="http://www.w3.org/1999/xsl/transform" xmlns:fo="http://www.w3.org/1999/xsl/format" exclude-result-prefixes="fo"> <xsl:output method="xml" version="1.0" omit-xml-declaration="no" indent="yes"/> <!-- ========================= --> <!-- root element: projectteam --> <!-- ========================= --> <xsl:template match="featuresrs"> <fo:root xmlns:fo="http://www.w3.org/1999/xsl/format"> ... 下一页