使用JDOM处理XML数据之PDF篇(一)
2008-04-25 13:43
489 查看
使用JDOM处理XML数据之PDF篇(一)XML:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /> 处理XML数据的三种方式我们已经都介绍过了, (直接读取http://www.csdn.net/Develop/read_article.asp?id=20720和使用XSLT http://www.csdn.net/Develop/read_article.asp?id=20733)现在来看一下第三种方式即转化成二进制格式(一般为PDF)的操作。这个操作要借助XML.apache.org/">http://XML.apache.org/ 提供的包fop来完成。其实这个是我整理的,不是自己写的:)它的代码如下:package XML;import java.io.*;import org.XML.sax.InputSource;import org.XML.sax.XMLReader;import org.apache.fop.apps.Driver;import org.apache.fop.apps.Version;import javax.XML.transform.*;import javax.XML.transform.stream.*;/** * Class to convert an XML document to PDF via * XSLT and XSL formatting objects. */public class PDFWriter { protected Transformer transformer = null; public PDFWriter () {}; public PDFWriter(StreamSource source) throws TransformerConfigurationException { // try { TransformerFactory factory = TransformerFactory.newInstance(); transformer = factory.newTransformer(source); /* } catch (TransformerConfigurationException tce) { throw new IllegalStateException("Stylesheet compilation problem: " + tce.getMessage()); } catch (TransformerFactoryConfigurationException tfce) { throw new IllegalStateException("JAXP configuration problem: " + tce.getMessage()); } */ } public PDFWriter(String xslFilePath) throws TransformerConfigurationException, FileNotFoundException { this( new StreamSource(new FileInputStream(xslFilePath)) ); } /** Invoke the ASF FOP engine to create the PDF */ protected byte[] invokeFOP(InputSource foSource) throws Exception { ByteArrayOutputStream out = new ByteArrayOutputStream(); Driver driver = new Driver(foSource, out); driver.run(); return out.toByteArray(); } /** Create a PDF from an XML file, using the stylesheet passed to the constructor. */ public byte[] generatePDF(String XMLFilePath) throws Exception { StreamSource XMLSource = new StreamSource( new FileInputStream(XMLFilePath) ); return generatePDF(XMLSource); } /** Does the interesting stuff */ public byte[] generatePDF(StreamSource XMLSource) throws Exception { ByteArrayOutputStream baos = new ByteArrayOutputStream(); StreamResult foResult = new StreamResult(baos); transformer.transform(XMLSource, foResult); ByteArrayInputStream bais = new ByteArrayInputStream( baos.toByteArray() ); return invokeFOP( new InputSource(bais) ); } /** To be used with files */ public static void createPDFFromXML(String xslFilePath, String XMLFilePath, String outputPDFPath) throws Exception { FileOutputStream fos = new FileOutputStream(outputPDFPath); createPDFFromXML(xslFilePath, new FileInputStream(XMLFilePath), fos); fos.close(); } /** To be called when there is no spoon (XML or PDF file) */ public static void createPDFFromXML(String xslFilePath, InputStream XMLIn, OutputStream PDFOut) throws Exception { PDFWriter writer = new PDFWriter(xslFilePath); byte[] PDFbytes = writer.generatePDF( new StreamSource(XMLIn) ); PDFOut.write(PDFbytes, 0, PDFbytes.length); } public static void main(String[] args) { String fileBasePath = "." + File.separator; String XMLFilePath = fileBasePath + "watchlist.XML"; String xslFilePath = fileBasePath + "watchlist2PDF.xsl"; String outputPDFPath = fileBasePath + "watchlist.PDF"; try { PDFWriter.createPDFFromXML(xslFilePath, XMLFilePath, outputPDFPath); } catch (Exception e) { System.out.println(e.getMessage()); e.printStackTrace(); } }} 这个javabean输入一个XSL-FO的文件的位置,一个XML文件的位置,输出一个PDF文件。
相关文章推荐
- 使用JDOM处理XML数据之PDF篇(二)
- 使用JDOM处理XML数据之XSLT篇(二)
- 在SSIS中使用XML任务处理XML数据或文档
- jQuery form插件的使用之处理server返回的JSON, XML,HTML数据
- Android开发之使用DefaultHandler处理XML数据
- 使用XStream注解处理xml的属性及数据集合
- jQuery form插件的使用--处理server返回的JSON, XML,HTML数据
- 在SSIS中使用XML任务处理XML数据或文档
- 使用jdom操作xml数据,生成含Jtree的applet(转载 Jagie 原创 )
- 使用jdom操作xml数据,生成含Jtree的applet
- Ajax 使用XMLHttpRequest对象发送数据和接收处理XML源代码
- 项目开发,我的名字不可能这么可爱——使用XML和Sqlite处理数据
- 如何使用Google APIs和Google应用系统集成(7)----在里面JSON兑换XML数据处理,JSON数据包括违规XML数据规范:XML节点名称不支持号码Java解
- 使用Jdom查询数据库后,把数据写入xml文件中
- 使用Java处理XML数据
- asp中使用MSXML2.DOMDocument处理XML数据时的注意事项
- xml数据处理--sax模块使用
- 使用XMLBeans处理XML数据和文档入门
- xml数据处理--ElementTree模块使用
- 使用 FOR XML 和 OPENXML 发布和处理 XML 数据 --[From MS]