2014-07-07 43 views

回答

1

您可以使用Smooks變壓器轉換成XML

的Smooks-CSV-Config中的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"> 
    <csv:reader fields="order_no?trim,cust_no?trim,prod_no?trim,amount?trim" 
    separator="|" quote="'" skipLines="0" rootElementName="orders" recordElementName="order" indent="true" /> 

    <resource-config selector="global-parameters"> 
     <param name="stream.filter.type">SAX</param> 
    </resource-config> 
</smooks-resource-list> 

在騾子的配置

<smooks:transformer 
     name="csvToXmlSmooksTransformer" 
     configFile="/transforms/smooks-csv-config.xml" 
     resultType="STRING" 
     reportPath="target/smooks-report/report.html" 
     /> 

然後創建Java中的tezt類

package net.pascalalma.mule.test; 

import java.io.File; 
import java.io.InputStream; 
import java.util.Locale; 
import java.util.TimeZone; 

import org.junit.Test; 
import org.mule.DefaultMuleMessage; 
import org.mule.api.MuleMessage; 
import org.mule.module.client.MuleClient; 
import org.mule.tck.FunctionalTestCase; 
import org.mule.util.IOUtils; 

public class SmooksCsvTest extends FunctionalTestCase 
{ 
    @Override 
    protected String getConfigResources() { 
     return "config/smooks-csv-config.xml"; 
    } 

    @Test 
    public void testSmooks() throws Exception { 
     InputStream in = IOUtils.getResourceAsStream("test-order.csv", this.getClass()); 

     MuleClient client = new MuleClient(); 
     MuleMessage reply = client.send("vm://test-csv-to-xml",new DefaultMuleMessage(in)); 

     assertNotNull(reply); 
     assertNotNull(reply.getPayload()); 

     Object payload = reply.getPayload(); 
     assertTrue("The message payload is not an instance of String", payload instanceof String); 
     assertTrue("The report file wasn't created", getReportFile().exists()); 
    } 

    private File getReportFile() { 
     return new File("target/smooks-report/report.html"); 
    } 
    private void deleteReportFile() { 
     getReportFile().delete(); 
    } 

    /* (non-Javadoc) 
    * @see org.mule.tck.AbstractMuleTestCase#doSetUp() 
    */ 
    @Override 
    protected void doSetUp() throws Exception { 
     super.doSetUp(); 

     TimeZone.setDefault(TimeZone.getTimeZone("EST")); 
     Locale.setDefault(new Locale("en","IE")); 
     deleteReportFile(); 
    } 

    /* (non-Javadoc) 
    * @see org.mule.tck.AbstractMuleTestCase#doTearDown() 
    */ 
    @Override 
    protected void doTearDown() throws Exception { 
     super.doTearDown(); 
     deleteReportFile(); 
    } 
} 

的輸出上面是這樣

<orders> 
     <order number="1"> 
       <order_no>1888852</order_no> 
       <cust_no>21625</cust_no> 
       <prod_no>02745011</prod_no> 
       <amount>31</amount> 
     </order> 
     <order number="2"> 
       <order_no>1888853</order_no> 
       <cust_no>21625</cust_no> 
       <prod_no>02745011</prod_no> 
       <amount>71</amount> 
     </order> 
     <order number="3"> 
       <order_no>1888854</order_no> 
       <cust_no>21625</cust_no> 
       <prod_no>02745011</prod_no> 
       <amount>3</amount> 
     </order> 
</orders> 

Reference

2

一到一個CSV轉換和映射爲XML的最簡單的方法是用騾子的DataMapper的 https://developer.mulesoft.com/docs/display/current/Datamapper+User+Guide+and+Reference

但,這Datamapper是Mule的一個特點企業版...

其他替代選擇是

  1. 負載從使用文件的入站端點本地盤使用表達變壓器分裂淨荷到每列中的CSV文件,

  2. ,將每列存儲在流變量,ref: - How to read CSV file and insert data into PostgreSQL using Mule ESB, Mule Studio

  3. 最後使用XSLT 創建XML有效載荷與流變量作爲輸入.. REF: - https://developer.mulesoft.com/docs/display/current/XSLT+Transformer

此外,您還可以參考以下鏈接: - http://opendevelopmentnotes.blogspot.in/2013/09/mule-esb-csv-to-xml-conversion.html

csv to xml: not sure the best way to do it in Mule ESB