SOAP structure
We already know that a SOAP message is an XML document, but let's have a better look by way of a diagram:
The following is a description of the components from the preceding diagram:
- Envelope: Mandatory element of SOAP message structure. Defines the start and the end of the message.
- Header: Optional element of the SOAP message. It contains information regarding the SOAP message that can be used to process the data.
- Body: This is the main part, which contains the actual message in XML structure. Obviously, it is a mandatory element.
- Fault: If any errors occur while processing the SOAP message, an optional Fault element can be used to provide information about them.
You must be thinking who exactly told us to follow this type of structure. Well, there is an organization named W3 that proposes standards for particular technologies. They have done the same for the SOAP structure.
You can easily find details about the SOAP envelope at http://www.w3.org/2001/12/soap-envelope. Likewise, you can see details about SOAP encoding and data types at http://www.w3.org/2001/12/soap-encoding.
Whatever we discuss about the structure of the SOAP message is defined by the W3 organization. However, this organization constantly investigates ways to optimize structures and bring in more robust specifications from time to time. So, we have to update with the latest specifications provided by them and implement them accordingly.
The following block depicts the general structure of a SOAP message:
<?xml version = "1.0"?>
<SOAP-ENV:Envelope xmlns:SOAP-ENV = "http://www.w3.org/2001/12/soap-envelope"
SOAP-ENV:encodingStyle = "http://www.w3.org/2001/12/soap-encoding">
<SOAP-ENV:Header>
...
...
</SOAP-ENV:Header>
<SOAP-ENV:Body>
...
...
<SOAP-ENV:Fault>
...
...
</SOAP-ENV:Fault>
...
</SOAP-ENV:Body>
</SOAP_ENV:Envelope>
The receiver is notified about the whole SOAP message with the indication of an envelope. What this means is, if the message received by the client has an envelope inside it, then the message is completely received and the client can parse and use it for further processing. Thus, the SOAP envelope plays the role of packaging the whole message.