如何将 CSV 的每一行处理为单个 XML 输出 - WSO2/Smooks
How to process each line of a CSV to a single XML output - WSO2/Smooks
TL;DR: 我有一个多行 CSV,我想逐行处理并为每一行生成不同的 XML。
我有以下 CSV:
Chathuri,Wimalasena,Female,25,SriLanka
Saminda,Wijerathne,Male,26,SriLanka
Dakshitha,Rathnayaka,Female,24,SriLanka
Harsha,Martin,Male,24,SriLanka
目前我正在使用 smooks 并进行以下转换:
<?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<people>
<person>
<lastname>Wimalasena</lastname>
<firstname>Chathuri</firstname>
<gender>Female</gender>
<age>25</age>
<country>SriLanka</country>
</person>
<person>
<lastname>Wijerathne</lastname>
<firstname>Saminda</firstname>
<gender>Male</gender>
<age>26</age>
<country>SriLanka</country>
</person>
<person>
<lastname>Rathnayaka</lastname>
<firstname>Dakshitha</firstname>
<gender>Female</gender>
<age>24</age>
<country>SriLanka</country>
</person>
<person>
<lastname>Martin</lastname>
<firstname>Harsha</firstname>
<gender>Male</gender>
<age>24</age>
<country>SriLanka</country>
</person>
</people>
</soapenv:Body>
</soapenv:Envelope>
问题是:我想要这样的东西:
<?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<people>
<person>
<lastname>Wimalasena</lastname>
<firstname>Chathuri</firstname>
<gender>Female</gender>
<age>25</age>
<country>SriLanka</country>
</person>
</people>
</soapenv:Body>
</soapenv:Envelope>
<?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<people>
<person>
<lastname>Wijerathne</lastname>
<firstname>Saminda</firstname>
<gender>Male</gender>
<age>26</age>
<country>SriLanka</country>
</person>
</people>
</soapenv:Body>
</soapenv:Envelope>
我的smook.xml:
<?xml version="1.0" encoding="UTF-8"?><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:ftl="http://www.milyn.org/xsd/smooks/freemarker-1.1.xsd">
<params>
<param name="stream.filter.type">SAX</param>
<param name="inputType">input.csv</param>
<param name="input.csv" type="input.type.actived">File:/C:\Users\victor.viola\Desktop\test.csv</param>
<param name="default.serialization.on">true</param>
</params>
<csv:reader fields="name,surname,gender,age,country"/>
<ftl:freemarker applyOnElement="#document">
<ftl:template><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
<people>
<#list .vars["csv-set"]["csv-record"] as csv_record>
<person>
<lastname>${.vars["csv_record"]["surname"]}</lastname>
<firstname>${.vars["csv_record"]["name"]}</firstname>
<gender>${.vars["csv_record"]["gender"]}</gender>
<age>${.vars["csv_record"]["age"]}</age>
<country>${.vars["csv_record"]["country"]}</country>
</person>
</#list>
</people>]]></ftl:template>
<param name="rootElementName">people</param>
<param name="modelSrc">File:/C:\Users\victor.viola\Desktop\example.xsd</param>
<param name="modelSrcType">XSD</param>
<param name="messageType">XML</param>
<param name="templateDataProvider">input</param>
</ftl:freemarker>
<resource-config selector="#document">
<resource>org.milyn.delivery.DomModelCreator</resource>
</resource-config>
</smooks-resource-list>
使用 smooks,您可以将消息路由到文件或 JMS 队列中。
请看一下这个以流模式读取文件并将其内容拆分为文件或 JMS 消息的示例(它适用于固定长度,但这与 csv 相同,只需使用 csv:reader )
代理服务:
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse" name="VFSProxy" transports="vfs" startOnLoad="true" trace="disable">
<target>
<inSequence>
<property name="DISABLE_SMOOKS_RESULT_PAYLOAD" value="true"/>
<smooks config-key="SmooksSplitFileFL">
<!--smooks config-key="SmooksSplitJMSFL"-->
<input type="text"/>
<output type="xml"/>
</smooks>
</inSequence>
</target>
<parameter name="transport.vfs.FileURI">vfs:file:///C:/In</parameter>
<parameter name="transport.vfs.FileNamePattern">.*.fl</parameter>
<parameter name="transport.vfs.ContentType">text/plain</parameter>
<parameter name="transport.vfs.Streaming">true</parameter>
<parameter name="transport.PollInterval">15</parameter>
<parameter name="transport.vfs.ActionAfterProcess">DELETE</parameter>
<parameter name="transport.vfs.ActionAfterFailure">DELETE</parameter>
</proxy>
拆分成文件的 Smooks(本地条目,名字用于命名文件,因此,如果有多行具有相同的名字,您将使用最后一个)
<?xml version="1.0" encoding="UTF-8"?>
<localEntry xmlns="http://ws.apache.org/ns/synapse" key="SmooksSplitFileFL">
<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" xmlns:fl="http://www.milyn.org/xsd/smooks/fixed-length-1.3.xsd" xmlns:file="http://www.milyn.org/xsd/smooks/file-routing-1.1.xsd">
<params>
<param name="stream.filter.type">SAX</param>
<param name="default.serialization.on">false</param>
</params>
<fl:reader fields="LASTNAME[2],FIRSTNAME[10]" skipLines="0"/>
<resource-config selector="record">
<resource>org.milyn.delivery.DomModelCreator</resource>
</resource-config>
<file:outputStream openOnElement="record" resourceName="fileSplitStream">
<file:fileNamePattern>${.vars["record"].LASTNAME}.xml</file:fileNamePattern>
<file:destinationDirectoryPattern>C:\Processed</file:destinationDirectoryPattern>
<file:highWaterMark mark="-1"/>
</file:outputStream>
<ftl:freemarker applyOnElement="record">
<ftl:template>/repository/resources/smooks/RecordToXML.ftl</ftl:template>
<ftl:use>
<ftl:outputTo outputStreamResource="fileSplitStream"/>
</ftl:use>
</ftl:freemarker>
</smooks-resource-list>
<description/>
</localEntry>
拆分为 JMS 消息的 Smooks
<?xml version="1.0" encoding="UTF-8"?>
<localEntry xmlns="http://ws.apache.org/ns/synapse" key="SmooksSplitJMSFL">
<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" xmlns:fl="http://www.milyn.org/xsd/smooks/fixed-length-1.3.xsd" xmlns:jms="http://www.milyn.org/xsd/smooks/jms-routing-1.2.xsd">
<params>
<param name="stream.filter.type">SAX</param>
<param name="default.serialization.on">false</param>
</params>
<fl:reader fields="LASTNAME[2],FIRSTNAME[10]" skipLines="0"/>
<resource-config selector="record">
<resource>org.milyn.delivery.DomModelCreator</resource>
</resource-config>
<jms:router routeOnElement="record" beanId="Person" destination="dynamicQueues/TestFL">
<jms:connection factory="QueueConnectionFactory"/>
<jms:jndi contextFactory="org.apache.activemq.jndi.ActiveMQInitialContextFactory" providerUrl="nio://localhost:61616"/>
<jms:highWaterMark mark="-1"/>
</jms:router>
<ftl:freemarker applyOnElement="record">
<ftl:template>/repository/resources/smooks/RecordToXML.ftl</ftl:template>
<ftl:use>
<ftl:bindTo id="Person"/>
</ftl:use>
</ftl:freemarker>
</smooks-resource-list>
<description/>
</localEntry>
模板(创建文件ESB_HOME/repository/resources/smooks/RecordToXML.ftl):
<#assign rec = .vars["record"]>
<person>
<lastname>${rec.LASTNAME}</lastname>
<firstname>${rec.FIRSTNAME}</firstname>
</person>
TL;DR: 我有一个多行 CSV,我想逐行处理并为每一行生成不同的 XML。
我有以下 CSV:
Chathuri,Wimalasena,Female,25,SriLanka
Saminda,Wijerathne,Male,26,SriLanka
Dakshitha,Rathnayaka,Female,24,SriLanka
Harsha,Martin,Male,24,SriLanka
目前我正在使用 smooks 并进行以下转换:
<?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<people>
<person>
<lastname>Wimalasena</lastname>
<firstname>Chathuri</firstname>
<gender>Female</gender>
<age>25</age>
<country>SriLanka</country>
</person>
<person>
<lastname>Wijerathne</lastname>
<firstname>Saminda</firstname>
<gender>Male</gender>
<age>26</age>
<country>SriLanka</country>
</person>
<person>
<lastname>Rathnayaka</lastname>
<firstname>Dakshitha</firstname>
<gender>Female</gender>
<age>24</age>
<country>SriLanka</country>
</person>
<person>
<lastname>Martin</lastname>
<firstname>Harsha</firstname>
<gender>Male</gender>
<age>24</age>
<country>SriLanka</country>
</person>
</people>
</soapenv:Body>
</soapenv:Envelope>
问题是:我想要这样的东西:
<?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<people>
<person>
<lastname>Wimalasena</lastname>
<firstname>Chathuri</firstname>
<gender>Female</gender>
<age>25</age>
<country>SriLanka</country>
</person>
</people>
</soapenv:Body>
</soapenv:Envelope>
<?xml version='1.0' encoding='utf-8'?><soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
<soapenv:Body>
<people>
<person>
<lastname>Wijerathne</lastname>
<firstname>Saminda</firstname>
<gender>Male</gender>
<age>26</age>
<country>SriLanka</country>
</person>
</people>
</soapenv:Body>
</soapenv:Envelope>
我的smook.xml:
<?xml version="1.0" encoding="UTF-8"?><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:ftl="http://www.milyn.org/xsd/smooks/freemarker-1.1.xsd">
<params>
<param name="stream.filter.type">SAX</param>
<param name="inputType">input.csv</param>
<param name="input.csv" type="input.type.actived">File:/C:\Users\victor.viola\Desktop\test.csv</param>
<param name="default.serialization.on">true</param>
</params>
<csv:reader fields="name,surname,gender,age,country"/>
<ftl:freemarker applyOnElement="#document">
<ftl:template><![CDATA[<?xml version="1.0" encoding="UTF-8"?>
<people>
<#list .vars["csv-set"]["csv-record"] as csv_record>
<person>
<lastname>${.vars["csv_record"]["surname"]}</lastname>
<firstname>${.vars["csv_record"]["name"]}</firstname>
<gender>${.vars["csv_record"]["gender"]}</gender>
<age>${.vars["csv_record"]["age"]}</age>
<country>${.vars["csv_record"]["country"]}</country>
</person>
</#list>
</people>]]></ftl:template>
<param name="rootElementName">people</param>
<param name="modelSrc">File:/C:\Users\victor.viola\Desktop\example.xsd</param>
<param name="modelSrcType">XSD</param>
<param name="messageType">XML</param>
<param name="templateDataProvider">input</param>
</ftl:freemarker>
<resource-config selector="#document">
<resource>org.milyn.delivery.DomModelCreator</resource>
</resource-config>
</smooks-resource-list>
使用 smooks,您可以将消息路由到文件或 JMS 队列中。
请看一下这个以流模式读取文件并将其内容拆分为文件或 JMS 消息的示例(它适用于固定长度,但这与 csv 相同,只需使用 csv:reader )
代理服务:
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse" name="VFSProxy" transports="vfs" startOnLoad="true" trace="disable">
<target>
<inSequence>
<property name="DISABLE_SMOOKS_RESULT_PAYLOAD" value="true"/>
<smooks config-key="SmooksSplitFileFL">
<!--smooks config-key="SmooksSplitJMSFL"-->
<input type="text"/>
<output type="xml"/>
</smooks>
</inSequence>
</target>
<parameter name="transport.vfs.FileURI">vfs:file:///C:/In</parameter>
<parameter name="transport.vfs.FileNamePattern">.*.fl</parameter>
<parameter name="transport.vfs.ContentType">text/plain</parameter>
<parameter name="transport.vfs.Streaming">true</parameter>
<parameter name="transport.PollInterval">15</parameter>
<parameter name="transport.vfs.ActionAfterProcess">DELETE</parameter>
<parameter name="transport.vfs.ActionAfterFailure">DELETE</parameter>
</proxy>
拆分成文件的 Smooks(本地条目,名字用于命名文件,因此,如果有多行具有相同的名字,您将使用最后一个)
<?xml version="1.0" encoding="UTF-8"?>
<localEntry xmlns="http://ws.apache.org/ns/synapse" key="SmooksSplitFileFL">
<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" xmlns:fl="http://www.milyn.org/xsd/smooks/fixed-length-1.3.xsd" xmlns:file="http://www.milyn.org/xsd/smooks/file-routing-1.1.xsd">
<params>
<param name="stream.filter.type">SAX</param>
<param name="default.serialization.on">false</param>
</params>
<fl:reader fields="LASTNAME[2],FIRSTNAME[10]" skipLines="0"/>
<resource-config selector="record">
<resource>org.milyn.delivery.DomModelCreator</resource>
</resource-config>
<file:outputStream openOnElement="record" resourceName="fileSplitStream">
<file:fileNamePattern>${.vars["record"].LASTNAME}.xml</file:fileNamePattern>
<file:destinationDirectoryPattern>C:\Processed</file:destinationDirectoryPattern>
<file:highWaterMark mark="-1"/>
</file:outputStream>
<ftl:freemarker applyOnElement="record">
<ftl:template>/repository/resources/smooks/RecordToXML.ftl</ftl:template>
<ftl:use>
<ftl:outputTo outputStreamResource="fileSplitStream"/>
</ftl:use>
</ftl:freemarker>
</smooks-resource-list>
<description/>
</localEntry>
拆分为 JMS 消息的 Smooks
<?xml version="1.0" encoding="UTF-8"?>
<localEntry xmlns="http://ws.apache.org/ns/synapse" key="SmooksSplitJMSFL">
<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" xmlns:fl="http://www.milyn.org/xsd/smooks/fixed-length-1.3.xsd" xmlns:jms="http://www.milyn.org/xsd/smooks/jms-routing-1.2.xsd">
<params>
<param name="stream.filter.type">SAX</param>
<param name="default.serialization.on">false</param>
</params>
<fl:reader fields="LASTNAME[2],FIRSTNAME[10]" skipLines="0"/>
<resource-config selector="record">
<resource>org.milyn.delivery.DomModelCreator</resource>
</resource-config>
<jms:router routeOnElement="record" beanId="Person" destination="dynamicQueues/TestFL">
<jms:connection factory="QueueConnectionFactory"/>
<jms:jndi contextFactory="org.apache.activemq.jndi.ActiveMQInitialContextFactory" providerUrl="nio://localhost:61616"/>
<jms:highWaterMark mark="-1"/>
</jms:router>
<ftl:freemarker applyOnElement="record">
<ftl:template>/repository/resources/smooks/RecordToXML.ftl</ftl:template>
<ftl:use>
<ftl:bindTo id="Person"/>
</ftl:use>
</ftl:freemarker>
</smooks-resource-list>
<description/>
</localEntry>
模板(创建文件ESB_HOME/repository/resources/smooks/RecordToXML.ftl):
<#assign rec = .vars["record"]>
<person>
<lastname>${rec.LASTNAME}</lastname>
<firstname>${rec.FIRSTNAME}</firstname>
</person>