Can someone give example how to convert csv to xml in mule?

﹥>﹥吖頭↗ 提交于 2019-12-22 09:59:26

问题


I am new to Mule.

I want to do below things

1) Read csv file from local drive
2) Transform to xml
3) Write xml

Please help.


回答1:


One of the easiest way to convert and map a CSV to XML is use of Mule Datamapper https://developer.mulesoft.com/docs/display/current/Datamapper+User+Guide+and+Reference

But, this Datamapper is a feature of Mule enterprise edition...

Other alternate option is to

  1. load the CSV file from local disc using File inbound endpoint,

  2. using expression-transformer to split the payload into each column, store each column in a flow variable, ref :- How to read CSV file and insert data into PostgreSQL using Mule ESB, Mule Studio

  3. and finally use XSLT to create XML payload with flow variables as input .. ref :- https://developer.mulesoft.com/docs/display/current/XSLT+Transformer

In addition, you can also refer following links :- 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




回答2:


You could use the Smooks Transformer for converting to 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>

in your mule config

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

then create the tezt class in java

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();
    }
}

The output of the above would look like this

<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



来源:https://stackoverflow.com/questions/24604599/can-someone-give-example-how-to-convert-csv-to-xml-in-mule

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!