Web services are applications or APIs that communicate over a network (typically the internet) using standard protocols like HTTP, and they allow for interactions between different systems. XML plays a significant role in web services as it acts as a universal format for data exchange. XML provides a way to structure data, making it easier for web services to send and receive information in a platform-independent and language-neutral manner.
Web services often use XML-based standards such as SOAP (Simple Object Access Protocol) and REST (Representational State Transfer), where XML is employed for data formatting and messaging.
Before diving into the details of XML’s role, let’s understand some fundamental terms:
SOAP is a protocol for exchanging structured information in web services using XML. It relies on XML to format messages and typically uses HTTP or SMTP for message transmission. SOAP is designed to handle complex operations and is highly extensible.
A SOAP message is an XML document consisting of the following parts:
12345
import javax.jws.WebService;
import javax.jws.WebMethod;
@WebService
public class PersonService {
@WebMethod
public String getPersonDetails(int personID) {
if (personID == 12345) {
return "John Doe ";
} else {
return "Person not found ";
}
}
}
JAX-WS allows you to publish a SOAP service easily in Java:
import javax.xml.ws.Endpoint;
public class Main {
public static void main(String[] args) {
Endpoint.publish("http://localhost:8080/person", new PersonService());
}
}
curl -X POST -H "Content-Type: text/xml" --data @request.xml http://localhost:8080/person
Where request.xml
contains the SOAP request shown earlier.
Output ?>
John
Doe
http://localhost:8080/person
.PersonID
in the body, and the service responds with the details in XML format.WSDL is an XML-based language used to describe the functionality and interface of a web service. A WSDL document describes the service location, operations (methods), and the format of request and response messages.
GetPersonDetails
).http://localhost:8080/person
).RESTful services are more lightweight and flexible than SOAP services. While REST can work with many formats (like JSON), it also supports XML as a data format. RESTful services make use of HTTP methods like GET
, POST
, PUT
, and DELETE
to perform CRUD operations.
from flask import Flask, request, jsonify
import xml.etree.ElementTree as ET
app = Flask(__name__)
@app.route('/person', methods=['POST'])
def get_person():
xml_data = request.data
root = ET.fromstring(xml_data)
person_id = root.find('PersonID').text
if person_id == '12345':
response = '''
John
Doe
'''
else:
response = 'Person not found '
return response, 200, {'Content-Type': 'application/xml'}
if __name__ == '__main__':
app.run(debug=True)
curl -X POST -H "Content-Type: text/xml" --data "12345 " http://localhost:5000/person
Output ?>
John
Doe
xml.etree.ElementTree
.PersonID
, it returns the person’s details or an error message.XML plays a crucial role in the landscape of web services, acting as a common format for data exchange between diverse systems and platforms. Its flexible and platform-independent nature makes it a reliable choice for communication in distributed environments. Web services, such as SOAP and REST, heavily rely on XML for encoding messages and facilitating interoperability between client-server architectures. Happy coding !❤️