Thursday, December 10, 2009

Send XML data over web service

Occasionally I am being asked a common question, "How to send XML data over web service?". In Java (in fact in any programming language) XML is just a string (sequence of characters). So sending XML as request parameter or receiving it in response is just like handling any other string data.

For the following XML data from web service client and server-

XML from client
<name>vinod</name>

XML from server
<greeting>Hello! vinod</greeting>

This is the web service (JAX-WS) request and response-
REQUEST
<?xml version="1.0" ?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
    <S:Body>
        <ns2:xmlData xmlns:ns2="http://vinodsingh.com">
            <data>&lt;name&gt;vinod&lt;/name&gt;</data>
        </ns2:xmlData>
    </S:Body>
</S:Envelope>

RESPONSE
<?xml version='1.0' encoding='UTF-8'?>
<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
    <S:Body>
        <ns2:xmlDataResponse xmlns:ns2="http://vinodsingh.com">
            <return>&lt;greeting>Hello! vinod&lt;/greeting></return>
        </ns2:xmlDataResponse>
    </S:Body>
</S:Envelope>

The above example clearly shows that JAX-WS runtime handles XML strings pretty well. The source code available here includes an example of sending bigger XML data.

3 Comments:

rama said...

Hi vinod,

i am using atomika with tomcat+springs,

in xa i am getting an error


com.atomikos.datasource.ResourceException: XA resource 'dataSource11':
resume for XID '636F6D2E61746F6D696B6F732E737072696E672E6A6462632E746D30303030313030303037:636F6D2E61746F6D696B6F732E737072696E672E6A6462632E746D31'
raised 0: unknown at com.atomikos.datasource.xa.XAResourceTransaction.resume(XAResourceTra

in my test server i am getting the issue 3 to 4 time a day.

i research on the issue in net some on told thatif we add

com.atomikos.icatch.serial_jta_transactions = false;

addove line in jta.properties this error wont repeat but i am getting the same error after writing that code in jta.props file.

please give me any solution.

urgent

ramakrishna

beanicatch said...

Hi Vinod,

I am not able to download the code though the provided link. Instead it is serving me WebserviceBlogCode.zip which I believe is not the correct file .

Vinod Singh said...

@beanicatch,

That contains code for sending XML data as well.