WSO2 ESB Smooks - XML到CSV转换

时间:2016-09-19 17:17:29

标签: wso2esb smooks

我有一个简单的代理服务,需要将XML有效负载转换为CSV消息。为此我正在使用smooks mediator。以下是我的代理服务。

<?xml version="1.0" encoding="UTF-8"?>
<proxy name="TestProxy" startOnLoad="true" trace="disable"
  transports="http https" xmlns="http://ws.apache.org/ns/synapse">
  <target>
    <inSequence>
      <log level="custom">
        <property name="STATUS" value="TESTING PROXY SERVICE"/>
      </log>
      <payloadFactory media-type="xml">
        <format>
              <csv-set>
                <csv-record number="2">
                  <Name>Jhone</Name>
                  <City>Colombo</City>
                  <Age>32</Age>
                </csv-record>
                <csv-record number="3">
                  <Name>Doe</Name>
                  <City>Jaffna</City>
                  <Age>32</Age>
                </csv-record>
              </csv-set>            
        </format>
        <args/>
      </payloadFactory>
      <log level="full"/>
      <smooks config-key="gov:Test/smooks/TestSmooksConfig.xml">
        <input type="xml"/>
        <output type="text"/>
      </smooks>
      <log level="custom">
        <property name="STATUS" value="PROCESSED MSG******"/>
      </log>
      <log level="full"/>
    </inSequence>
    <outSequence/>
    <faultSequence/>
  </target>
</proxy>

以下是我的Smooks配置。

<?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="inputType">input.xml</param>
    <param name="stream.filter.type">SAX</param>
    <param name="input.xml" type="input.type.actived">File:/F:\Practicals\WSO2\vfs_file_listening\input-csv\b.xml</param>
  </params>
  <ftl:freemarker applyOnElement="#document">
    <ftl:template><![CDATA["Name","City","Age"
<#list .vars["csv-set"]["csv-record"] as csv_record>
"${.vars["csv_record"]["Name"]}","${.vars["csv_record"]["City"]}","${.vars["csv_record"]["Age"]}"
</#list>]]></ftl:template>
    <param name="includeFieldNames">true</param>
    <param name="seperator">,</param>
    <param name="quote">"</param>
    <param name="csvFields">Name,City,Age</param>
    <param name="messageType">CSV</param>
    <param name="templateDataProvider">input</param>
  </ftl:freemarker>
  <resource-config selector="#document">
    <resource>org.milyn.delivery.DomModelCreator</resource>
  </resource-config>
</smooks-resource-list>

输出消息仅打印以下内容。

<?xml version='1.0' encoding='utf-8'?>
<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
    <soapenv:Body>
        <ax:text xmlns:ax="http://ws.apache.org/commons/ns/payload">"Name","City","Age"</ax:text>
    </soapenv:Body>
</soapenv:Envelope>

没有输入XML标签中的数据,任何人都可以指出我做错了吗?

提前致谢。

1 个答案:

答案 0 :(得分:2)

我通过以下Smooks配置实现了这一点。 (在wso2 dev支持的帮助下,感谢Evanthika)。

<?xml version="1.0" encoding="UTF-8"?>
<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">
    <params>
        <param name="stream.filter.type">SAX</param>
        <param name="inputType">input.xml</param>
        <param name="input.xml" type="input.type.actived">File:/C:\Work\2016\09_ESB_Auto_Mail_rpts\TestFiles\TestFileXMLCSVMap.xml
        </param>
        <param name="default.serialization.on">true</param>
    </params>
    <resource-config selector="csv-set,csv-record">
        <resource>org.milyn.delivery.DomModelCreator</resource>
    </resource-config>
    <ftl:freemarker applyOnElement="csv-record">
        <ftl:template>
            <!-- <#ftl ns_prefixes={"D":"http://ws.apache.org/ns/synapse"}> ${.vars["csv-record"].Name},${.vars["csv-record"].City},${.vars["csv-record"].Age} -->
        </ftl:template>
        <param name="quote">"</param>
        <param name="includeFieldNames">true</param>
        <param name="csvFields">Name,City,Age</param>
        <param name="seperator">,</param>
        <param name="messageType">CSV</param>
        <param name="templateDataProvider">input</param>
    </ftl:freemarker>
</smooks-resource-list>

已完成的更改是,

    带有xml命名空间的
  • <ftl:template>。 (在我的情况下,它来自数据服务,因此http://ws.apache.org/ns/synapse标记中的<#ftl ns_prefixes>命名空间。

  • <resource-config selector="csv-set,csv-record"><ftl:freemarker applyOnElement="csv-record">指出了 smooks的元素。

希望这有助于任何人参考。