扫一扫
分享文章到微信
扫一扫
关注官方公众号
至顶头条
作者:中国IT实验室 来源:中国IT实验室 2007年10月7日
关键字: ORACLE
在本页阅读全文(共6页)
该示例数据模板 (catalogDataTemplate.xml) 用于从数据库表生成 XML 文档:
catalogDataTemplate.xml
<?xml version="1.0" encoding="WINDOWS-1252" ?> <dataTemplate name="catalogDataTemplate" description="Magazine Catalog" defaultPackage="" Version="1.0"> <parameters> <parameter name="id" dataType="character" /> </parameters> <dataQuery> <sqlStatement name="Q1"> <![CDATA[ SELECT CatalogId, Journal, Publisher, Edition, Title, Author from OE.CATALOG WHERE CatalogId=:id </sqlStatement> </dataQuery> <dataStructure> <group name="G_Catalog" source="Q1"> <element name="CATALOGID" value="CatalogId" /> <element name="JOURNAL" value="Journal" /> <element name="PUBLISHER" value="Publisher" /> <element name="EDITION" value="Edition" /> <element name="TITLE" value="Title"/> <element name="AUTHOR" value="Author" /> </group> </dataStructure> </dataTemplate>
示例数据模板指定的参数、id 均为 character 类型。SQL 查询中的绑定变量用于设置 CatalogId 列的值(通过 id 参数)。CatalogId 列的值在运行时设置。示例查询中的所有列都是 VARCHAR 类型。可以在 SQL 查询中指定以下列类型:VARCHAR2、CHAR、NUMBER、DATE、TIMESTAMP、BLOB、CLOB 和 XMLType。
接下来,我们将通过 Data Engine API 使用数据库数据生成 XML 文档。首先,创建一个 DataProcessor 对象:
DataProcessor dataProcessor = new DataProcessor();
在 DataProcessor 对象上设置数据模板:
dataProcessor.setDataTemplate("catalogDataTemplate.xml");
指定参数的值,并在 DataProcessor 对象上设置参数:
Hashtable parameters = new Hashtable(); parameters.put("id","catalog1"); dataProcessor.setParameters(parameters);
创建 Oracle 数据库的 JDBC 连接,并在 DataProcessor 对象上设置该连接:
Class.forName("oracle.jdbc.OracleDriver"); String url="jdbc:oracle:thin:@localhost:1521:ORCL"; java.sql.Connection jdbcConnection = DriverManager.getConnection(url, "OE", "password"); dataProcessor.setConnection(jdbcConnection);
设置输出 XML 文档并运行数据处理器:
dataProcessor.setOutput("catalogData.xml"); dataProcessor.processData();
数据库将生成 XML 文档 (catalogData.xml),如下所示:
<?xml version="1.0" encoding="UTF-8"?> <catalogDataTemplate> <id>catalog1</id> <LIST_G_CATALOG> <G_CATALOG> <CATALOGID>catalog1</CATALOGID> <JOURNAL>Oracle Magazine</JOURNAL> <PUBLISHER>Oracle Publishing</PUBLISHER> <EDITION>March-April 2006</EDITION> <TITLE>Commanding ASM</TITLE> <AUTHOR>Arup Nanda</AUTHOR> </G_CATALOG> </LIST_G_CATALOG> </catalogDataTemplate>
以下是 XMLPublisher.java 类:
package xmlpublisher; import com.sun.java.util.collections.Hashtable; import oracle.apps.xdo.XDOException; import oracle.apps.xdo.common.pdf.util.PDFDocMerger; import oracle.apps.xdo.dataengine.DataProcessor; import oracle.apps.xdo.template.FOProcessor; import oracle.apps.xdo.template.fo.util.FOUtility; import oracle.xml.parser.v2.NSResolver; import java.io.FileInputStream; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.InputStream; import java.lang.reflect.Array; import java.sql.DriverManager; import java.sql.SQLException; public class XMLPublisher { public XMLPublisher() { } public void foProcessorEngine() { try { FOProcessor processor = new FOProcessor(); processor.setData("catalog.xml"); processor.setTemplate("catalog.xsl"); processor.setOutput("catalog.pdf"); processor.setOutputFormat(FOProcessor.FORMAT_PDF); processor.generate(); } catch (XDOException e) { System.out.println("XDOException " + e.getMessage()); } } public void xslFoUtility() { try { InputStream[] input = new InputStream[2]; InputStream firstFOStream = FOUtility.createFO("catalog2.xml", "catalog.xsl"); InputStream secondFOStream = FOUtility.createFO("catalog3.xml", "catalog.xsl"); Array.set(input, 0, firstFOStream); Array.set(input, 1, secondFOStream); InputStream mergedFOStream = FOUtility.mergeFOs(input, null); if (mergedFOStream == null) { System.out.println("Merge failed."); } FOProcessor processor = new FOProcessor(); processor.setData(mergedFOStream); processor.setTemplate((String) null); processor.setOutput("catalog2.pdf"); processor.setOutputFormat(FOProcessor.FORMAT_PDF); processor.generate(); } catch (XDOException e) { System.out.println("XDOException" + e.getMessage()); } } public void pdfDocumentMerger() { try { FileInputStream[] inputStreams = new FileInputStream[2]; inputStreams[0] = new FileInputStream("catalog.pdf"); inputStreams[1] = new FileInputStream("catalog2.pdf"); FileOutputStream outputStream = new FileOutputStream("catalog3.pdf"); PDFDocMerger pdfMerger = new PDFDocMerger(inputStreams, outputStream); pdfMerger.setPageNumberCoordinates(300, 20); pdfMerger.setPageNumberFontInfo("Courier", 10); pdfMerger.setPageNumberValue(1, 1); pdfMerger.process(); pdfMerger = null; } catch (XDOException e) { System.out.println("XDOException" + e.getMessage()); } catch (FileNotFoundException e) { System.out.println("FileNotFoundException " + e.getMessage()); } } public void dataEngine() { try { Class.forName("oracle.jdbc.OracleDriver"); String url = "jdbc:oracle:thin:@localhost:1521:ORCL"; java.sql.Connection jdbcConnection = DriverManager.getConnection(url, "OE", "password"); DataProcessor dataProcessor = new DataProcessor(); dataProcessor.setDataTemplate("catalogDataTemplate.xml"); Hashtable parameters = new Hashtable(); parameters.put("id", "catalog1"); dataProcessor.setParameters(parameters); dataProcessor.setConnection(jdbcConnection); dataProcessor.setOutput("catalogData.xml"); dataProcessor.processData(); } catch (SQLException e) { System.out.println("SQLException " + e.getMessage()); } catch (ClassNotFoundException e) { System.out.println("ClassNotFoundException " + e.getMessage()); } catch (XDOException e) { System.out.println("XDOException" + e.getMessage()); } } public static void main(String[] argv) { XMLPublisher xmlPublisher = new XMLPublisher(); xmlPublisher.foProcessorEngine(); xmlPublisher.xslFoUtility(); xmlPublisher.pdfDocumentMerger(); xmlPublisher.dataEngine(); } }
如果您非常迫切的想了解IT领域最新产品与技术信息,那么订阅至顶网技术邮件将是您的最佳途径之一。
现场直击|2021世界人工智能大会
直击5G创新地带,就在2021MWC上海
5G已至 转型当时——服务提供商如何把握转型的绝佳时机
寻找自己的Flag
华为开发者大会2020(Cloud)- 科技行者