Granny's Programming Pearls
"inside of every large program is a small program struggling to get out"
JavaRanch.com/granny.jsp
The moose likes Web Services and the fly likes Sending XML  data to webservice Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Java » Web Services
Bookmark "Sending XML  data to webservice" Watch "Sending XML  data to webservice" New topic
Author

Sending XML data to webservice

Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8904

I have a requirement where in I need to send XML data as one of the argument to a web service. The XML contains some policy details.
Since there is no XML data type that I can define, I was wondering if I pass XML as a string but semantics of XML will lost in this case. Will it cause difficulty in parsing the XML string at the server? The XML string must be validated against a schema.
Will Documemt/Literal type of SOAP encoding solve my peoblem, or is there some other way.
Thanks
Pradeep


Groovy
Raj Chila
Ranch Hand

Joined: Mar 18, 2004
Posts: 128

Hi Pradeep,
As such sending XML Data as a String Parameter to a call on Webservice does not pose any problems.
We have used String Represntations of XML Data, infact we even used encryption / Decryption of the String and all worked fine, including the Validation of the file after decryption.
May be you are worried about the size of the XML file...., we probably did not have a huge file...it was about 20 - 25 lines containing customer data.
I think you will have to write a Bean representation of your XML File, you can make use of JAXB for the same.
Balaji Loganathan
author and deputy
Bartender

Joined: Jul 13, 2001
Posts: 3150
Other simple and ugly way could be to send the xml data as base64 binary (btye array) and valid it at the receiver side.
but the best way is use to the bean representation that rajini as suggested.


Spritle Software Blogs
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8904

If I get a XML string at the server side how do I parse it?
Balaji Loganathan
author and deputy
Bartender

Joined: Jul 13, 2001
Posts: 3150
This http://test.javaranch.com/wiki/view?XmlFaq will help you.
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
Originally posted by Pradeep Bhat:
Will Documemt/Literal type of SOAP encoding solve my peoblem
Yes, as it's you who's responsible for doing all XML parsing -- you get a single element into your Java code which you need to interpret.
Something like


Author of Test Driven (2007) and Effective Unit Testing (2013) [Blog] [HowToAskQuestionsOnJavaRanch]
Raj Chila
Ranch Hand

Joined: Mar 18, 2004
Posts: 128

Lasse,
if you are refering to be a webservice method, then I think it will not be possible to pass an Element object as a SOAP Message.
All the Objects that we intend to send accross the network to invoke business services should be converted into a SOAP message (also referred to as the Serialization and Deserialization), as far as I know we can exchange the primitive java types and some utility classes in Java, and all the Other Objects which follow the Bean Patterns...(ie., Default constructor, getter and setter methods).
Element neither satisfies the Bean Patterns (it is an interface I think), nor are there any Serializers or Deserializers I think in either JWSDP or AXIS).
so in that case you will have to create your own serializers / Deserializers, but that will mean creating a dependency and also B2B integration will become difficult as also the cross platform integration.
what best we can do is we could construct Java Objects from the XML(DTD) Files on the server side and use these Objects to create the WSDL, and then you can also provide a "client.jar" files that contains the same beans so that the construction of the beans for serialization is easier on the client side, or the client side can generate using its own processes.
Lasse Koskela
author
Sheriff

Joined: Jan 23, 2002
Posts: 11962
    
    5
RajaniKanth, web service methods which take one Element as input and return an Element are how message-style web services are mapped to Java code.
JeanLouis Marechaux
Ranch Hand

Joined: Nov 12, 2001
Posts: 906
RajaniKanth,
Lasse is definitively right.
You should consider reading again about Web Services and Document/Literal.
An Element is an correct datatype for web services using literal encoding style


/ JeanLouis<br /><i>"software development has been, is, and will remain fundamentally hard" (Grady Booch)</i><br /> <br />Take a look at <a href="http://www.epfwiki.net/wikis/openup/" target="_blank" rel="nofollow">Agile OpenUP</a> in the Eclipse community
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8904

Yes, as it's you who's responsible for doing all XML parsing -- you get a single element into your Java code which you need to interpret.

Is the single element the SOAP body contents?
JeanLouis Marechaux
Ranch Hand

Joined: Nov 12, 2001
Posts: 906
Originally posted by Pradeep Bhat:

Is the single element the SOAP body contents?

Yes it is.
In Document/Literal, the Body element only contains the XML document you exchange through your web service.
This XML document is in plain text. It is Literal
The difference with RPC/Literal is that in RPC, the XML is enclosed in a method name. (The method you call in a RPC way)
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8904

Where can I get sample code of how to send document SOAP messages and how to parse them?
JeanLouis Marechaux
Ranch Hand

Joined: Nov 12, 2001
Posts: 906
Originally posted by Pradeep Bhat:
Where can I get sample code of how to send document SOAP messages and how to parse them?

You can download Axis and study the code samples.
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8904

Originally posted by Balaji Loganathan:
This http://test.javaranch.com/wiki/view?XmlFaq will help you.


Balaji,

The above link is broken. could you please provide me the correct link.
Thanks
Balaji Loganathan
author and deputy
Bartender

Joined: Jul 13, 2001
Posts: 3150
I dont know why i pointed that link to you
Anyway here it is http://faq.javaranch.com/view?XmlFaq
Karthik Mohan
Greenhorn

Joined: Sep 29, 2004
Posts: 23
A good way of attacking this problem would be to use an API such as XMLBeans or JAXB (XMLBeans has more advs than JAXB, i think).

Design a Schema file for your input request message and output response message and generate the appropriate serializers/de-serializers using XMLBeans and it is very easy to use also. You can use the same classes to program your client also. As the whole schema is represented as a Java Object, it is very easy to retrieve data and construct the response also.
Pradeep bhatt
Ranch Hand

Joined: Feb 27, 2002
Posts: 8904

Thanks guys.
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Sending XML data to webservice
 
Similar Threads
UTC format date in XML- Help!!
Unicode characters - javax . xml . transform . Transformer
Unicode characters - javax . xml . transform . Transformer
Web service return complex data type vs. XML string
Java: Preserve empty string