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

类别:Java 点击:0 评论:0 推荐:
                 使用JDOM处理XML数据之PDF篇(二)

XML文件的代码和

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转化成二进制格式。

本文地址:http://com.8s8s.com/it/it17210.htm