如何使用 WSO2 ESB 4.8.1 中的 Smooks 从 csv 文件中读取用双引号引起来的字段
How to read fields enclosed in double quotes using Smooks in WSO2 ESB 4.8.1 from a csv file
我有一个简单的代理服务,它从 csv 文件读取并使用 smooks 调解器将其转换为 XML 消息。如果 csv 文件包含用单引号 (') 括起来的字段,那么效果很好。
文件结构:
'accountid','accountname','accountlocation'
'121','dummyaccount','finland'
'121','dummyaccount','finland'
'121','dummyaccount','finland'
但不幸的是,在我的例子中,这些字段包含在 "double quotes" 中并且 Smooks 没有解析文件,它无法读取文件。
文件结构双引号:
"accountid","accountname","accountlocation"
"121","dummyaccount","finland"
"121","dummyaccount","finland"
"121","dummyaccount","finland"
代理服务:
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="ConvertcsvfiletoXML"
transports="vfs"
startOnLoad="true"
trace="disable">
<description/>
<target>
<inSequence>
<property name="DISABLE_SMOOKS_RESULT_PAYLOAD" value="true"/>
<smooks config-key="gov:/SmooksTest/smooks-config.xml">
<input type="text"/>
<output type="xml"/>
</smooks>
</inSequence>
</target>
<parameter name="transport.PollInterval">100</parameter>
<parameter name="transport.vfs.ActionAfterProcess">MOVE</parameter>
<parameter name="transport.vfs.FileURI">file:///home/omerkhalid/Documents/SmooksTest/in</parameter>
<parameter name="transport.vfs.MoveAfterProcess">file:///home/omerkhalid/Documents/SmooksTest/out</parameter>
<parameter name="transport.vfs.MoveAfterFailure">file:///home/omerkhalid/Documents/SmooksTest/failed</parameter>
<parameter name="transport.vfs.FileNamePattern">.*.csv</parameter>
<parameter name="transport.vfs.ContentType">text/plain</parameter>
<parameter name="transport.vfs.ActionAfterFailure">MOVE</parameter>
</proxy>
Smooks 配置文件:
<smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
xmlns:csv="http://www.milyn.org/xsd/smooks/csv-1.1.xsd"
xmlns:jms="http://www.milyn.org/xsd/smooks/jms-routing-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="stream.filter.readerPoolSize">300</param>
</params>
<csv:reader fields="id,name,location" separator="," quote="'" skipLines="1" />
<resource-config selector="csv-record">
<resource>org.milyn.delivery.DomModelCreator</resource>
</resource-config>
<ftl:freemarker applyOnElement="csv-record">
<ftl:template>/repository/resources/smooks/csv_record_as_xml.ftl</ftl:template>
<ftl:use>
<ftl:bindTo id="csv_record_as_xml"/>
</ftl:use>
</ftl:freemarker>
<jms:router routeOnElement="csv-record" beanId="csv_record_as_xml" destination="TestQueue">
<jms:message>
<!-- Need to use special FreeMarker variable ".vars" -->
<jms:correlationIdPattern>${.vars["csv-record"].id}</jms:correlationIdPattern>
</jms:message>
<jms:jndi properties="/repository/resources/smooks/activemq.sr.jndi.properties" />
<jms:highWaterMark mark="10000000"/>
</jms:router>
</smooks-resource-list>
对于单引号,以下配置有效:
<csv:reader fields="id,name,location" separator="," quote="'" skipLines="1" />
但是对于双引号,以下配置不起作用:
<csv:reader fields="id,name,location" separator="," quote=" " " skipLines="1" />
那么遇到双引号怎么办呢?
注意:在我的例子中,将双引号更改为另一个特殊符号不是开放式的。
"Double quotes" 是默认值,所以您只需删除节点 "csv:reader"
中的属性 "quote"
另一种解决方案是使用特殊字符来指定此值:
<csv:reader fields="id,name,location" separator="," quote=""" skipLines="1"/>
我有一个简单的代理服务,它从 csv 文件读取并使用 smooks 调解器将其转换为 XML 消息。如果 csv 文件包含用单引号 (') 括起来的字段,那么效果很好。
文件结构:
'accountid','accountname','accountlocation'
'121','dummyaccount','finland'
'121','dummyaccount','finland'
'121','dummyaccount','finland'
但不幸的是,在我的例子中,这些字段包含在 "double quotes" 中并且 Smooks 没有解析文件,它无法读取文件。
文件结构双引号:
"accountid","accountname","accountlocation"
"121","dummyaccount","finland"
"121","dummyaccount","finland"
"121","dummyaccount","finland"
代理服务:
<?xml version="1.0" encoding="UTF-8"?>
<proxy xmlns="http://ws.apache.org/ns/synapse"
name="ConvertcsvfiletoXML"
transports="vfs"
startOnLoad="true"
trace="disable">
<description/>
<target>
<inSequence>
<property name="DISABLE_SMOOKS_RESULT_PAYLOAD" value="true"/>
<smooks config-key="gov:/SmooksTest/smooks-config.xml">
<input type="text"/>
<output type="xml"/>
</smooks>
</inSequence>
</target>
<parameter name="transport.PollInterval">100</parameter>
<parameter name="transport.vfs.ActionAfterProcess">MOVE</parameter>
<parameter name="transport.vfs.FileURI">file:///home/omerkhalid/Documents/SmooksTest/in</parameter>
<parameter name="transport.vfs.MoveAfterProcess">file:///home/omerkhalid/Documents/SmooksTest/out</parameter>
<parameter name="transport.vfs.MoveAfterFailure">file:///home/omerkhalid/Documents/SmooksTest/failed</parameter>
<parameter name="transport.vfs.FileNamePattern">.*.csv</parameter>
<parameter name="transport.vfs.ContentType">text/plain</parameter>
<parameter name="transport.vfs.ActionAfterFailure">MOVE</parameter>
</proxy>
Smooks 配置文件:
<smooks-resource-list xmlns="http://www.milyn.org/xsd/smooks-1.1.xsd"
xmlns:csv="http://www.milyn.org/xsd/smooks/csv-1.1.xsd"
xmlns:jms="http://www.milyn.org/xsd/smooks/jms-routing-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="stream.filter.readerPoolSize">300</param>
</params>
<csv:reader fields="id,name,location" separator="," quote="'" skipLines="1" />
<resource-config selector="csv-record">
<resource>org.milyn.delivery.DomModelCreator</resource>
</resource-config>
<ftl:freemarker applyOnElement="csv-record">
<ftl:template>/repository/resources/smooks/csv_record_as_xml.ftl</ftl:template>
<ftl:use>
<ftl:bindTo id="csv_record_as_xml"/>
</ftl:use>
</ftl:freemarker>
<jms:router routeOnElement="csv-record" beanId="csv_record_as_xml" destination="TestQueue">
<jms:message>
<!-- Need to use special FreeMarker variable ".vars" -->
<jms:correlationIdPattern>${.vars["csv-record"].id}</jms:correlationIdPattern>
</jms:message>
<jms:jndi properties="/repository/resources/smooks/activemq.sr.jndi.properties" />
<jms:highWaterMark mark="10000000"/>
</jms:router>
</smooks-resource-list>
对于单引号,以下配置有效:
<csv:reader fields="id,name,location" separator="," quote="'" skipLines="1" />
但是对于双引号,以下配置不起作用:
<csv:reader fields="id,name,location" separator="," quote=" " " skipLines="1" />
那么遇到双引号怎么办呢? 注意:在我的例子中,将双引号更改为另一个特殊符号不是开放式的。
"Double quotes" 是默认值,所以您只需删除节点 "csv:reader"
中的属性 "quote"另一种解决方案是使用特殊字符来指定此值:
<csv:reader fields="id,name,location" separator="," quote=""" skipLines="1"/>