TL; DR:我有一個多行CSV,我想逐行處理併爲每行生成不同的XML。如何將CSV的每一行處理爲單個XML輸出 - WSO2/Smooks
我有以下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>
無關的問題,但不是'.vars [「csv_record」] [「surname」]'你可以寫'csv_record.surname'。 '.vars [「csv-set」] [「csv-record」]'因爲'-':'csv \ -set.csv \ -record'更復雜('\ -'需要FreeMarker 2.3.22+) 。 – ddekany
謝謝你的提示=) –