Smooks转换设计 背景 不同的合作银行对应的外部接口是不一样的,我们需要把外部这些变化不定的接口格式,转换为我们银保通系统可以识别的内部接口.Smooks可以很好的解决这一问题.并且,当合作银行的接口随着业务的变化而发生变化时,smooks只需要通过变更转换模板,就可以实现对变化后接口的支持。 Smooks框架可以在smooks的官网www.smooks.org (需要翻墙软)下载。本文smooks的版本是smooks-1.4。在其lib文件夹中是所需要的jar包。将所有jar包导入你的工程中,就能使用smooks进行报文解析了。 现在说明下面两种种转换:XmlToXml,CsvToXml. 1. Xml To Xml 说明xml转换为xml在smooks的配置中有两种方式,一种是通过xsl模板控制,一种是通过freemaker模板控制。其中后者比较灵活,格式容易控制,推荐使用后者。 1.1 use xsl Templating 工程名为MyXmlToXml ①.需要转换的报文:文件名在程序中定为input-message.xml <?xml version="1.0" encoding="GBK"?> <PACKET type="REQUEST" version="1.0"> <HEAD> <TRANSTYPE>SYN</TRANSTYPE> <TRANSCODE>100006</TRANSCODE> <USER>eies</USER> <PASSWORD>eies</PASSWORD> <SVCSEQNO></SVCSEQNO > </HEAD> <THIRD> <EXTENTERPCODE>19002020102</EXTENTERPCODE> <EXTPROVCODE>24</EXTPROVCODE> <EXTAREACODE>0187</EXTAREACODE> <EXTSITECODE>0187</EXTSITECODE> <EXTSEQUENCENO>200804060001</EXTSEQUENCENO> <TRANSDATE>2009-08-06</TRANSDATE> <TRANSTIME>12:30:30</TRANSTIME> <EXTOPERATORCODE>2020</EXTOPERATORCODE> </THIRD> <BODY> <POLICYNO>110880829200904000016</POLICYNO> <C_PASSWORD /> </BODY> </PACKET> ②.配置转化的配置文件,在程序中名为smooks-config.xml: <?xml version='1.0' encoding='UTF-8'?> <smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.0.xsd"> <!-- HEAD --> <resource-config selector="HEAD"> <resource type="xsl"> <![CDATA[ <HEAD> <SVCTYPE><xsl:value-of select="./TRANSTYPE" /></SVCTYPE> <SVCCODE><xsl:value-of select="./TRANSCODE" /></SVCCODE> <SVCNAME><xsl:value-of select="./USER" /></SVCNAME> <SVCSEQNO><xsl:value-of select="./PASSWORD" /></SVCSEQNO> <SVCSTATUS>000000</SVCSTATUS> <SVCMESSAGE></SVCMESSAGE> <SVCDETAIL>QueryPolicyByProposalNoService</SVCDETAIL> </HEAD> ]]> </resource> <param name="is-xslt-templatelet">true</param> </resource-config> <!-- THIRD --> <resource-config selector="THIRD"> <resource type="xsl"> <![CDATA[ <THIRD> <EXTENTERPCODE><xsl:value-of select="./EXTENTERPCODE" /></EXTENTERPCODE> <EXTPROVCODE><xsl:value-of select="./EXTPROVCODE" /></EXTPROVCODE> <EXTAREACODE><xsl:value-of select="./EXTAREACODE" /></EXTAREACODE> <EXTSITECODE><xsl:value-of select="./EXTSITECODE" /></EXTSITECODE> <EXTSEQUENCENO><xsl:value-of select="./EXTSEQUENCENO" /></EXTSEQUENCENO> <TRANSDATE><xsl:value-of select="./TRANSDATE" /></TRANSDATE> <TRANSTIME><xsl:value-of select="./TRANSTIME" /></TRANSTIME> <EXTOPERATORCODE><xsl:value-of select="./EXTOPERATORCODE" /></EXTOPERATORCODE> </THIRD> ]]> </resource> <param name="is-xslt-templatelet">true</param> </resource-config> <!-- BODY --> <resource-config selector="BODY"> <resource type="xsl"> <![CDATA[ <BASE_LIST> <BASE_DATA> <C_PLY_NO><xsl:value-of select="./POLICYNO" /></C_PLY_NO> <C_PASSWORD><xsl:value-of select="./C_PASSWORD" /></C_PASSWORD> </BASE_DATA> </BASE_LIST> ]]> </resource> <param name="is-xslt-templatelet">true</param> </resource-config> </smooks-resource-list> 说明此配置文件的作用是通过内置xsl的配置将input-message.xml中的一些标签名替换掉,例如: 将<TRANSTYPE>替换成</SVCTYPE>,新增加一些丢失的标签例如<SVCDETAIL>QueryPolicyByProposalNoService</SVCDETAIL>,并且将<BODY> 标签下的内容改为一个list类型的标签。结果请对照下面out.xml。 ③.通过如下程序可读取输入报文input-message.xml和配置文件smooks-config.xml,并产生输出文件out.xml. package example; import org.milyn.Smooks; import org.milyn.SmooksException; import org.milyn.io.StreamUtils; import org.xml.sax.SAXException; import javax.xml.transform.stream.StreamSource; import javax.xml.transform.stream.StreamResult; import java.io.*; public class Main { //读入输入报文 private static byte[] messageIn = readInputMessage(); /** * 主方法 */ public static void main(String[] args) throws IOException, SAXException, SmooksException { String messageOut = Main.runSmooksTransform(); StreamUtils.writeFile(new File("out.xml"),messageOut.getBytes()); //将输出报文写出到文件out.xml } /** * 根据配置文件输入报文的byte[]转化为输出报文的String * 利用smooks进行转化 */ protected static String runSmooksTransform() throws IOException, SAXException, SmooksException { // 读取配置文件smooks-config.xml Smooks smooks = new Smooks("smooks-config.xml"); try { CharArrayWriter outputWriter = new CharArrayWriter(); //存入smooks的转化结果存入CharArrayWriter的对象,该类继承java.io.writer //调用smooks.filterSource方法,进行smooks转化。 smooks.filterSource(new StreamSource(new ByteArrayInputStream(messageIn)), new StreamResult(outputWriter)); return outputWriter.toString(); //将转化结果转化为字符串 } finally { smooks.close(); } } /** * 读入输入报文 * @return 输入报文的byte[] */ private static byte[] readInputMessage() { try { return StreamUtils.readStream(new FileInputStream("input-message.xml")); } catch (IOException e) { e.printStackTrace(); return "<no-message/>".getBytes(); } } } ④.输出的报文out.xml: <PACKET type="REQUEST" version="1.0"> <HEAD> <SVCTYPE>SYN</SVCTYPE> <SVCCODE>100006</SVCCODE> <SVCNAME>eies</SVCNAME> <SVCSEQNO>eies</SVCSEQNO> <SVCSTATUS>000000</SVCSTATUS> <SVCMESSAGE></SVCMESSAGE> <SVCDETAIL>QueryPolicyByProposalNoService</SVCDETAIL> </HEAD> <THIRD> <EXTENTERPCODE>19002020102</EXTENTERPCODE> <EXTPROVCODE>24</EXTPROVCODE> <EXTAREACODE>0187</EXTAREACODE> <EXTSITECODE>0187</EXTSITECODE> <EXTSEQUENCENO>200804060001</EXTSEQUENCENO> <TRANSDATE>2009-08-06</TRANSDATE> <TRANSTIME>12:30:30</TRANSTIME> <EXTOPERATORCODE>2020</EXTOPERATORCODE> </THIRD> <BASE_LIST> <BASE_DATA> <C_PLY_NO>110880829200904000016</C_PLY_NO> <C_PASSWORD></C_PASSWORD> </BASE_DATA> </BASE_LIST> </PACKET> 1.2 use freemaker Templating 工程名为:MyXmlToXmlWithFreemaker ①.需要转换的报文:文件名在程序中定为input-message.xml <order id='332'> <header> <customer number="123">Joe</customer> </header> <order-items> <order-item id='1'> <product>1</product> <quantity>2</quantity> <price>8.80</price> </order-item> <order-item id='2'> <product>2</product> <quantity>3</quantity> <price>10.90</price> </order-item> <!--可能还有 --> </order-items> </order> ②.配置转化的配置文件,在程序中名为smooks-config.xml: <?xml version="1.0"?> <smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd" xmlns:ftl="http://www.milyn.org/xsd/smooks/freemarker-1.1.xsd"> <!-- Filter the message using the SAX Filter (i.e. not DOM, so no intermediate DOM for the "complete" message - there are "mini" DOMs for the NodeModels below).... --> <params> <param name="stream.filter.type">SAX</param> <param name="default.serialization.on">false</param> </params> <!-- Create 2 NodeModels. One high level model for the "order" (header etc) and then one per "order-item". These models are used in the FreeMarker templating resources defined below. You need to make sure you set the selector such that the total memory footprint is as low as possible. In this example, the "order" model will contain everything accept the <order-item> data (the main bulk of data in the message). The "order-item" model only contains the current <order-item> data (i.e. there's max 1 order-item in memory at any one time). --> <resource-config selector="order,order-item"> <resource>org.milyn.delivery.DomModelCreator</resource> </resource-config> <!-- Apply the first part of the template when we reach the start of the <order-items> element. Apply the second part when we reach the end. Note the <?TEMPLATE-SPLIT-PI?> Processing Instruction in the template. This tells Smooks where to split the template, resulting in the order-items being inserted at this point. --> <ftl:freemarker applyOnElement="order-items"> <ftl:template><!--<?xml version="1.0" encoding="GBK"?> <salesorder type="REQUEST" version="1.0"> <details> <orderid>${order.@id}</orderid> <customer> <id>${order.header.customer.@number}</id> <name>${order.header.customer}</name> </customer> </details> <itemList> <?TEMPLATE-SPLIT-PI?> </itemList> </salesorder>--> </ftl:template> </ftl:freemarker> <!-- Output the <order-items> elements. This will appear in the output message where the <?TEMPLATE-SPLIT-PI?> token appears in the order-items template. --> <ftl:freemarker applyOnElement="order-item"> <ftl:template><!-- <item> <id>${.vars["order-item"].@id}</id> <productId>${.vars["order-item"].product}</productId> <quantity>${.vars["order-item"].quantity}</quantity> <price>${.vars["order-item"].price}</price> </item>--> </ftl:template> </ftl:freemarker> </smooks-resource-list> 说明此配置文件的作用是通过内置freemaker模板将input-message.xml中的格式进行转换。结果请对照下面out.xml。 ③.通过如下程序可读取输入报文input-message.xml和配置文件smooks-config.xml,并产生输出文件out.xml. package example; import org.milyn.Smooks; import org.milyn.SmooksException; import org.milyn.io.StreamUtils; import org.xml.sax.SAXException; import javax.xml.transform.stream.StreamResult; import javax.xml.transform.stream.StreamSource; import java.io.*; /** * smooks转换类 * @author zhangzheng */ public class Main { //读入输入报文 private static byte[] messageIn = readInputMessage(); /** * 主方法 */ public static void main(String[] args) throws IOException, SAXException, SmooksException { String messageOut=Main.runSmooks(); //调用smooks转换方法 System.out.println(messageOut); //打印输出结果 StreamUtils.writeFile(new File("out.xml"), messageOut.getBytes()); //将输出报文写出到文件out.xml } /** * 根据配置文件输入报文的byte[]转化为输出报文的String * 利用smooks进行转化 */ protected static String runSmooks() throws IOException, SAXException, SmooksException { // 读取配置文件smooks-config.xml Smooks smooks = new Smooks("smooks-config.xml"); CharArrayWriter outputWriter = new CharArrayWriter(); //存入smooks的转化结果存入CharArrayWriter的对象,该类继承java.io.writer try { //调用smooks.filterSource方法,进行smooks转化。 smooks.filterSource(new StreamSource(new ByteArrayInputStream(messageIn)), new StreamResult(outputWriter)); return outputWriter.toString(); } finally { smooks.close(); } } private static byte[] readInputMessage() { try { return StreamUtils.readStream(new FileInputStream("input-message.xml")); } catch (IOException e) { e.printStackTrace(); return "<no-message/>".getBytes(); } } } ④.输出的报文out.xml: <?xml version="1.0" encoding="GBK"?> <salesorder type="REQUEST" version="1.0"> <details> <orderid>332</orderid> <customer> <id>123</id> <name>Joe</name> </customer> </details> <itemList> <item> <id>1</id> <productId>1</productId> <quantity>2</quantity> <price>8.80</price> </item> <item> <id>2</id> <productId>2</productId> <quantity>3</quantity> <price>10.90</price> </item> </itemList> </salesorder> 2. Csv To Xml Csv转化成xml的配置也有两种(xsl和freemaker),由于freemaker模板易于控制,下面介绍freemaker方法。 工程名:MyCsvToXml ①.需要转换的报文:文件名在程序中定为input-message.csv 说明cvs文件一般是用","分隔,如果分隔符为"|"符号,需要在配置文件smooks-config.xml <csv:reader> 中增加一个属性separator="|",对于其他分隔符号同样适用。 SYN,100006,eies,eies,19002020102,24,0187,0187,200804060001,2009-08-06,12:30:30,2020,110880829200904000016, ②.配置转化的配置文件,在程序中名为smooks-config.xml: <?xml version="1.0"?> <smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd" xmlns:csv="http://www.milyn.org/xsd/smooks/csv-1.2.xsd" xmlns:core="http://www.milyn.org/xsd/smooks/smooks-core-1.3.xsd" xmlns:ftl="http://www.milyn.org/xsd/smooks/freemarker-1.1.xsd"> <csv:reader fields="SVCTYPE,SVCCODE,SVCNAME,SVCSEQNO, EXTENTERPCODE,EXTPROVCODE,EXTAREACODE,EXTSITECODE, EXTSEQUENCENO,TRANSDATE,TRANSTIME,EXTOPERATORCODE, C_PLY_NO,C_PASSWORD" rootElementName="PACKET" recordElementName="PACKET"> <csv:singleBinding beanId="PACKET" class="java.util.HashMap"/> </csv:reader> <!-- Use the SAX filter - more memory efficient and handle huge streams. --> <core:filterSettings type="SAX" /> <!-- Apply a FreeMarker template to each CSV record, generating a different XML structure based on the gender value... --> <ftl:freemarker applyOnElement="PACKET"> <ftl:template><!--<PACKET type="REQUEST" version="1.0"> <HEAD> <SVCTYPE>${PACKET.SVCTYPE}</SVCTYPE> <SVCCODE>${PACKET.SVCCODE}</SVCCODE> <SVCNAME>${PACKET.SVCNAME}</SVCNAME> <SVCSEQNO>${PACKET.SVCSEQNO}</SVCSEQNO> <SVCSTATUS>000000</SVCSTATUS> <SVCMESSAGE></SVCMESSAGE> <SVCDETAIL>QueryPolicyByProposalNoService</SVCDETAIL> </HEAD> <THIRD> <EXTENTERPCODE>${PACKET.EXTENTERPCODE}</EXTENTERPCODE> <EXTPROVCODE>${PACKET.EXTPROVCODE}</EXTPROVCODE> <EXTAREACODE>${PACKET.EXTAREACODE}</EXTAREACODE> <EXTSITECODE>${PACKET.EXTSITECODE}</EXTSITECODE> <EXTSEQUENCENO>${PACKET.EXTSEQUENCENO}</EXTSEQUENCENO> <TRANSDATE>${PACKET.TRANSDATE}</TRANSDATE> <TRANSTIME>${PACKET.TRANSTIME}</TRANSTIME> <EXTOPERATORCODE>${PACKET.EXTOPERATORCODE}</EXTOPERATORCODE> </THIRD> <BASE_LIST> <BASE_DATA> <C_PLY_NO>${PACKET.C_PLY_NO}</C_PLY_NO> <C_PASSWORD>${PACKET.C_PASSWORD}</C_PASSWORD> </BASE_DATA> </BASE_LIST> </PACKET> --> </ftl:template> <ftl:use> <ftl:inline directive="replace"/> </ftl:use> </ftl:freemarker> </smooks-resource-list> 说明此配置文件的总用是将input-message.csv中的信息加上相应的标签头,转换成xml,并在生成的xml 中加入csv中缺失的标签。结果请对照下面out.xml。 ③.通过如下程序可读取输入报文input-message.csv和配置文件smooks-config.xml,运行程序,产生输出文件out.xml. package example; import org.milyn.Smooks; import org.milyn.SmooksException; import org.milyn.io.StreamUtils; import org.xml.sax.SAXException; import javax.xml.transform.stream.StreamSource; import javax.xml.transform.stream.StreamResult; import java.io.*; /** * csvToXMl * @author zhangzheng */ public class Main { //读入输入报文 private static byte[] messageIn = readInputMessage(); /** * 主方法 */ public static void main(String[] args) throws IOException, SAXException, SmooksException { String messageOut = Main.runSmooksTransform(); StreamUtils.writeFile(new File("out.xml"), messageOut.getBytes()); } /** * 根据配置文件输入报文的byte[]转化为输出报文的String * 利用smooks进行转化 */ protected static String runSmooksTransform() throws IOException, SAXException, SmooksException { Smooks smooks = new Smooks("smooks-config.xml"); try { StringWriter writer = new StringWriter(); smooks.filterSource(new StreamSource(new InputStreamReader(new ByteArrayInputStream(messageIn), "UTF-8")), new StreamResult(writer)); return writer.toString(); } finally { smooks.close(); } } /** * 读入输入报文 * @return 输入报文的byte[] */ private static byte[] readInputMessage() { try { return StreamUtils.readStream(new FileInputStream("input-message.csv")); } catch (IOException e) { e.printStackTrace(); return "<no-message/>".getBytes(); } } } ④.输出的报文out.xml: <PACKET type="REQUEST" version="1.0"> <HEAD> <SVCTYPE>SYN</SVCTYPE> <SVCCODE>100006</SVCCODE> <SVCNAME>eies</SVCNAME> <SVCSEQNO>eies</SVCSEQNO> <SVCSTATUS>000000</SVCSTATUS> <SVCMESSAGE></SVCMESSAGE> <SVCDETAIL>QueryPolicyByProposalNoService</SVCDETAIL> </HEAD> <THIRD> <EXTENTERPCODE>19002020102</EXTENTERPCODE> <EXTPROVCODE>24</EXTPROVCODE> <EXTAREACODE>0187</EXTAREACODE> <EXTSITECODE>0187</EXTSITECODE> <EXTSEQUENCENO>200804060001</EXTSEQUENCENO> <TRANSDATE>2009-08-06</TRANSDATE> <TRANSTIME>12:30:30</TRANSTIME> <EXTOPERATORCODE>2020</EXTOPERATORCODE> </THIRD> <BASE_LIST> <BASE_DATA> <C_PLY_NO>110880829200904000016</C_PLY_NO> <C_PASSWORD></C_PASSWORD> </BASE_DATA> </BASE_LIST> </PACKET> 3. 拓展 Smooks 还可以进行Json, EDI 等常用数据格式的文件转化。具体方法请参加从smooks官网下载下来的smooks-1.4文件夹中/docs/SmooksUserGuide_v1.4.html的说明。具体的程序可以参加examples下的例子。