Web Services:
Using XML in Web Services:
Web services allow communication between applications over the internet or a network. XML is key in web services as it provides a standard format for data exchange.
One of the most common protocols that uses XML for this purpose is SOAP (Simple Object Access Protocol).
SOAP (Simple Object Access Protocol):
- XML-Based: Uses XML to encode messages.
- Communication: Between web services and clients.
- Transport: Usually HTTP or HTTPS.
- Message Structure: Envelope, header and body.
Example of a SOAP Message:
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:example="http://www.example.com">
<soapenv:Header/>
<soapenv:Body>
<example:getBook>
<example:bookId>001</example:bookId>
</example:getBook>
</soapenv:Body>
</soapenv:Envelope>
In this example:
- Envelope: Defines the start and end of the message.
- Header: Contains optional metadata.
- Body: Contains the actual data being transmitted, such as a request for a book's information.
Understanding WSDL (Web Services Description Language)
WSDL (Web Services Description Language) is an XML based language to describe the functionality of a web service. It provides a standard way to specify the operations of the service, the messages and how to access the service.
Key Components of WSDL:
- Types: Define the data types used by the service.
- Messages: Describe the messages between client and server.
- Port Types: Define the operations (methods) of the service.
- Bindings: Specify the protocol and message format.
- Service: Define the service endpoint (URL).
Example of a WSDL Document:
<definitions xmlns="http://schemas.xmlsoap.org/wsdl/"
xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/"
xmlns:tns="http://www.example.com"
targetNamespace="http://www.example.com">
<types>
<xsd:schema targetNamespace="http://www.example.com">
<xsd:element name="getBookRequest" type="xsd:string"/>
<xsd:element name="getBookResponse" type="tns:Book"/>
<xsd:complexType name="Book">
<xsd:sequence>
<xsd:element name="title" type="xsd:string"/>
<xsd:element name="author" type="xsd:string"/>
<xsd:element name="price" type="xsd:decimal"/>
</xsd:sequence>
</xsd:complexType>
</xsd:schema>
</types>
<message name="getBookRequest">
<part name="bookId" element="xsd:string"/>
</message>
<message name="getBookResponse">
<part name="book" element="tns:Book"/>
</message>
<portType name="BookServicePortType">
<operation name="getBook">
<input message="tns:getBookRequest"/>
<output message="tns:getBookResponse"/>
</operation>
</portType>
<binding name="BookServiceBinding" type="tns:BookServicePortType">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<operation name="getBook">
<soap:operation soapAction="getBook"/>
<input>
<soap:body use="literal"/>
</input>
<output>
<soap:body use="literal"/>
</output>
</operation>
</binding>
<service name="BookService">
<port name="BookServicePort" binding="tns:BookServiceBinding">
<soap:address location="http://www.example.com/BookService"/>
</port>
</service>
</definitions>
Interacting with XML-Based Web Services:
XML web services require requests and responses in XML, usually via SOAP. Here’s how to do it in different languages.
JavaScript (using fetch):
const soapRequest = `
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:example="http://www.example.com">
<soapenv:Header/>
<soapenv:Body>
<example:getBook>
<example:bookId>001</example:bookId>
</example:getBook>
</soapenv:Body>
</soapenv:Envelope>
`;
fetch("http://www.example.com/BookService", {
method: "POST",
headers: {
"Content-Type": "text/xml"
},
body: soapRequest
})
.then(response => response.text())
.then(responseXML => {
console.log(responseXML);
})
.catch(error => {
console.error("Error:", error);
});
Python (using requests):
import requests
soap_request = """
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:example="http://www.example.com">
<soapenv:Header/>
<soapenv:Body>
<example:getBook>
<example:bookId>001</example:bookId>
</example:getBook>
</soapenv:Body>
</soapenv:Envelope>
"""
headers = {"Content-Type": "text/xml"}
response = requests.post("http://www.example.com/BookService", data=soap_request, headers=headers)
print(response.text)
Java (using HttpURLConnection):
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.nio.charset.StandardCharsets;
public class SOAPClient {
public static void main(String[] args) throws Exception {
String soapRequest = """
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:example="http://www.example.com">
<soapenv:Header/>
<soapenv:Body>
<example:getBook>
<example:bookId>001</example:bookId>
</example:getBook>
</soapenv:Body>
</soapenv:Envelope>
""";
URL url = new URL("http://www.example.com/BookService");
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.setRequestProperty("Content-Type", "text/xml; charset=utf-8");
conn.setDoOutput(true);
try (OutputStream os = conn.getOutputStream()) {
os.write(soapRequest.getBytes(StandardCharsets.UTF_8));
}
int responseCode = conn.getResponseCode();
System.out.println("Response Code: " + responseCode);
}
}