Reputation: 19425
I need to create a web service that returns XML data to some of my clients.
Where ever I google for web service tutorials, 80% talks about using SOAP.
My question is just this: Can any client read the fetched data as ordinary XML?
Are there alternatives for SOAP?
Upvotes: 2
Views: 258
Reputation: 5591
I'd say it heavily depends on your requirements.
JSON is lightweight, as it does not have as much overhead as XML. If you have lots of short key-value parts, the most bytes that are transferred in a XML are the tag and attribute definitions, not the information data itself.
JSON is well suited for use in JavaScript Applications. However, most programming language support JSON natively; among them, popular languages like PHP or - since Flash 10.2 - Actionscript 3.
XML lives in a rich ecosphere of well defined (and widely spread) other standards. Among them:
XSD, a schema definition where you can define how your information is structured and how clients can expect them to be. These can be accompanied by namespace. Many programming languages (such as Java) or Frameworks (such as .NET) can auto generate classes and objects based on this schemas.
XSL(T) is a transformation Language, that can transform you're given XML into a document format that can suit your clients automatically (e.g. different HTML Snippets for different parts of their website).
SOAP takes heavily advantage of these standards, as it provide schemes and well defined contracts about how clients can consume your service. Many Languages (among them PHP, Java) and many frameworks (among them .NET and Zend Framework) can auto-detect which interfaces a service offers and autogenerate an object oriented interface without the need for the client to interpret the SOAP protocol - it's so well defined that it can be done automatically. Some frameworks (e.g. Zend, .NET) even provide to automatically generate a SOAP service for you, once you have defined your classes - thanks to a very well defined standard.
REST, as jmort253 already said, is the other approach: You do not have that much of standards and restrictions and are therefore very free in your implementation.
To make a long story short: I recommend to use SOAP for complex information that has to be transported as data and REST for non complex data, that can mainly be seen as key => value pairs without many dimensions.
A good example for SOAP is the adidas miCoach API, that has to handle heavy amounts of workout data and has to handly heavy amounts of workout results.
Twitter, on the other hand, handles mainly key => value information (such as tags => messages) and suits perfectly to a REST API.
Upvotes: 2
Reputation: 34038
REST is a lightweight alternative to SOAP. It uses existing HTTP methods like GET, POST, PUT, DELETE. Conceptually, a URL identifies a resource on your server, and the HTTP method defines the action to be performed.
REST is an architecture style, so it's flexible in terms of how you implement it. The architecture is just a guideline where an application is considered RESTful if it meets the defined characteristics of a REST web service.
JSON is commonly used as the transport, instead of XML. The advantage is that JavaScript can instantly parse JSON, and many server side technologies have libraries for deserializing JSON into an object. While JSON is recommended, you can use any content type with REST, including XML.
See the Wikipedia article on REST for more details.
Examples:
Get Car class for Ford Thunderbird
HTTP GET http://example.com/cars/ford/thunderbird/8NUY1234
$.ajax({
url: '/cars/ford/thunderbird/8NUY1234',
type: "GET", /* implicit default, listed for clarity only */
success: function(data) {
alert(JSON.stringify(data) ); /* output json
alert("engine = " + data.engine);
}
});
Insert a vehicle in the collection
HTTP POST http://example.com/cars/ford/f150/?vin=F14564564564&color=blue&engine=I6&...
// insert record on server.
var dataString = "vin=F143567547&color=blue&engine=I6"
$.ajax({
url: '/cars/ford/f150',
data: dataString,
type: "POST",
success: function(data) {
}
});
Upvotes: 6