(0228) 5552576-0


info@thomas-bayer.com

CSV to XML Converter Web Service

This free SOAP Web Service can transform your CSV documents into XML. A delimiter that separates the entries in the CSV file can be specified. The converter can extract the names for the XML elements from the first line of the CSV document.

The service uses the SOAP Message Transmission Optimization Mechanism in the response so your client must be able to deal with MTOM. At the end of this page you'll find Java code for an Axis2 client using MTOM.

Web Services Training

Learn the basic concepts of Web Services and Web Services Security by participating in our Web Services Training for Managers or in our Web Services Security Course.

Usage

The Web Service uses Base64Binary XML Schema type to transfer the source csv text to the Web Service and to transfer the result XML document back. The usage of MTOM for the request is optional but the service will respond with a MTOM encoded message.

WSDL:

http://www.thomas-bayer.com/axis2/services/CSV2XMLService?wsdl

Request

The listing below shows a soap request for the CSV2XML Web Service. The element csvtext is mandatory. The elements delimiter, firstLineAreColumnNames and stringDelimiter are optional.

<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
    <soapenv:Body>
        <ns1:convert xmlns:ns1="http://thomas-bayer.com/cx/">
            <ns1:delimiter>;</ns1:delimiter>
            <ns1:firstLineAreColumnNames>true</ns1:firstLineAreColumnNames>
            <ns1:stringDelimiter>'</ns1:stringDelimiter>
            <ns1:csvtext>GFGst23dGbld934</ns1:csvtext>            
        </ns1:convert>
    </soapenv:Body>
</soapenv:Envelope>
Listing 1: SOAP Request

Response

The listing below shows a response from the CSV2XML Web Service using MTOM.

HTTP/1.1 200 OK 
Server: Apache-Coyote/1.1
Content-Type: multipart/related; 
              boundary=MIMEBoundaryurn_uuid_BB29392B983F0DC5861174657454507;
              type="application/xop+xml";
              start="<0.urn:uuid:BB29392B983F0DC5861174657454508@apache.org>";
              start-info="application/soap+xml";charset=UTF-8
Transfer-Encoding: chunked
Date: Fri, 23 Mar 2007 13:44:14 GMT

--MIMEBoundaryurn_uuid_BB29392B983F0DC5861174657454507
content-type: application/xop+xml; charset=UTF-8; type="application/soap+xml";
content-transfer-encoding: binary
content-id: <0.urn:uuid:BB29392B983F0DC5861174657454508@apache.org>

<soapenv:Envelope xmlns:soapenv="http://www.w3.org/2003/05/soap-envelope">
    <soapenv:Header />
    <soapenv:Body>
        <ns1:convertResponse xmlns:ns1="http://thomas-bayer.com/cx/">
            <ns1:xmldata>
                <xop:Include href="cid:1.urn:uuid:BB29392B983F0DC5861174657454509@apache.org"
                 xmlns:xop="http://www.w3.org/2004/08/xop/include" />
            </ns1:xmldata>
        </ns1:convertResponse>
    </soapenv:Body>
</soapenv:Envelope>

--MIMEBoundaryurn_uuid_BB29392B983F0DC5861174657454507
content-type: application/octet-stream 
content-transfer-encoding: binary
content-id: <1.urn:uuid:BB29392B983F0DC5861174657454509@apache.org>

<csvimport>
  <row>
    <Vorname>Max</Vorname>
    <Name>Mustermann</Name>
    <Ort>Mannheim</Ort>
    <PLZ>68209</PLZ>
    <Strasse>Musterstrasse 23</Strasse>
    <Text>Mein Text mit ;Sonderzeichen</Text>
    <Bemerkung>meine Bemerkungen</Bemerkung>
  </row>
  <row>
    <Vorname>Gerda</Vorname>
    <Name>Gans</Name>
    <Ort>Mannheim</Ort>
    <PLZ>68209</PLZ>
    <Strasse>Musterstrasse 20</Strasse>
    <Text>mein</Text>
    <Bemerkung>meine zweiten Bemerkungen</Bemerkung>
  </row>
</csvimport>
Listing 2: Response with MTOM Optimization

Sample Axis2 Client

The CSV2XML service can be used with this simple Java client based on AXIOM.

package com.thomas_bayer.csv2xml;

import org.apache.axiom.om.OMAbstractFactory;
import org.apache.axiom.om.OMElement;
import org.apache.axiom.om.OMFactory;
import org.apache.axiom.om.OMNamespace;
import org.apache.axiom.om.OMText;
import org.apache.axis2.Constants;
import org.apache.axis2.addressing.EndpointReference;
import org.apache.axis2.client.Options;
import org.apache.axis2.client.ServiceClient;
import org.apache.axis2.util.Base64;

public class SimpleCSV2XMLClient {

    private static final OMFactory fac = OMAbstractFactory.getOMFactory();

    private static final OMNamespace ns = fac.createOMNamespace(
			"http://thomas-bayer.com/cx/", "ns");

    final static String csv = "Id;Name;City\n" + "123;Peter;Munich\n"
			+ "124;Paul;Cologne\n" + "125;Hans;Berlin";

    public static void main(String[] args) throws Exception {

        OMElement convert = fac.createOMElement("convert", ns);
        OMElement csvtext = fac.createOMElement("csvtext", ns);
        OMElement titles = fac.createOMElement("firstLineAreColumnNames", ns);
        titles.setText("true");
        csvtext.addChild(fac.createOMText(Base64.encode(csv.getBytes())));
        convert.addChild(titles);
        convert.addChild(csvtext);

        ServiceClient sender = new ServiceClient();
        Options options = new Options();
        options.setTo(new EndpointReference(
				"http://localhost:2001/axis2/services/CSV2XMLService"));
        options.setProperty(Constants.Configuration.ENABLE_MTOM,
				Constants.VALUE_TRUE);
        sender.setOptions(options);

        OMElement result = sender.sendReceive(convert);

        OMText xml = (OMText) result.getFirstElement().getFirstOMChild();
        Base64.decode(xml.getText(), System.out);

    }

}
Listing 3: Axis2 Client using MTOM

Acknowledgment

This Web Service is based on OIO's CSV to XML converter which can be used as a library, WebStart application or ant task.

The Web Service is hosted on an Apache Axis2 SOAP engine.

Terms and Conditions

Use of this web service is free of charge for both personal and commercial purposes. We can change or deactivate the service at any time without notice. We shall not be liable for any consequence to anyone or anything which results from changing or discontinuing this free service. You shall not use of this free service for illegal purposes. The right to use this service is revocable by us at any time. These rights shall be subject to the laws of the Federal Republic of Germany.

WE HEREBY DISCLAIM ANY AND ALL WARRANTIES, EXPRESS OR IMPLIED, RELATIVE TO THE FREE SERVICE, INCLUDING BUT NOT LIMITED TO ANY WARRANTY OF FITNESS FOR A PARTICULAR PURPOSE OR MERCHANTIBILITY. WE SHALL NOT BE LIABLE OR RESPONSIBLE FOR ANY DAMAGES, INJURIES OR LIABILITIES CAUSED DIRECTLY OR INDIRECTLY FROM THE USE OF THE FREE SERVICE, INCLUDING BUT NOT LIMITED TO INCIDENTAL, CONSEQUENTIAL OR SPECIAL DAMAGES.