Skip to content

SOAP Messages — Structure and Anatomy of XML-Based Communication

DodaTech Updated 2026-06-28 2 min read

In this tutorial, you will learn about SOAP Messages. We cover key concepts, practical examples, and best practices to help you master this topic.

SOAP messages are structured XML documents with four main elements: the Envelope (root), Header (metadata), Body (payload), and Fault (error information).

What You'll Learn

  • The structure of SOAP messages
  • Each element's purpose and content rules
  • How to construct valid SOAP messages

Why It Matters

Every SOAP web service exchanges messages with this structure. Understanding it is essential for debugging and building SOAP integrations.

flowchart TD
    A["SOAP Envelope\n(Root element)"] --> B["SOAP Header\n(Optional metadata)"]
    A --> C["SOAP Body\n(Required payload)"]
    C --> D["SOAP Fault\n(Optional error)"]
    B --> E["Authentication"]
    B --> F["Transaction IDs"]
    B --> G["Routing info"]
    C --> H["Request/Response data"]
    style A fill:#dbeafe,stroke:#2563eb

Code Examples

<!-- Complete SOAP message structure -->
<?xml version="1.0" encoding="UTF-8"?>
<soap:Envelope
    xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
    soap:encodingStyle="http://schemas.xmlsoap.org/soap/encoding/">
  <soap:Header>
    <m:Transaction xmlns:m="http://example.com/transaction">
      <m:ID>TX-2024-001</m:ID>
      <m:Timestamp>2024-01-15T10:30:00Z</m:Timestamp>
    </m:Transaction>
  </soap:Header>
  <soap:Body>
    <p:PlaceOrder xmlns:p="http://example.com/order">
      <p:CustomerID>12345</p:CustomerID>
      <p:ItemID>ABC-789</p:ItemID>
      <p:Quantity>2</p:Quantity>
    </p:PlaceOrder>
  </soap:Body>
</soap:Envelope>
# Building SOAP messages programmatically
from xml.etree.ElementTree import Element, SubElement, tostring

def build_soap_envelope(body_element):
    envelope = Element('{http://schemas.xmlsoap.org/soap/envelope/}Envelope')
    body = SubElement(envelope, '{http://schemas.xmlsoap.org/soap/envelope/}Body')
    body.append(body_element)
    return tostring(envelope, encoding='unicode')

order = Element('{http://example.com/order}PlaceOrder')
SubElement(order, '{http://example.com/order}CustomerID').text = '12345'
SubElement(order, '{http://example.com/order}ItemID').text = 'ABC-789'

soap_message = build_soap_envelope(order)
print(soap_message)

Common Mistakes

1. Missing the Envelope Namespace

Every SOAP message must declare the SOAP envelope namespace.

2. Optional Header Omitted Incorrectly

Some services require specific headers. Omitting them causes errors.

3. Fault Handled as Regular Body Content

SOAP Faults have a specific structure. Parse them correctly.

4. Incorrect Encoding Style

The soap:encodingStyle attribute must match the service's expectations.

5. Nested Envelopes

SOAP messages cannot have nested Envelope elements.

Practice Questions

  1. What is the root element of every SOAP message?
  2. What is the purpose of the SOAP Header?
  3. When is a SOAP Fault included in the message?
  4. What encoding style does SOAP typically use?
  5. Can the SOAP Body be empty?

Answers:

  1. soap:Envelope.
  2. To carry metadata like authentication, Transaction IDs, and routing info.
  3. When an error occurs processing the SOAP message.
  4. http://schemas.xmlsoap.org/soap/encoding/ or literal XML.
  5. Yes, for some operations that only require header information.

Challenge: Write a SOAP message for a flight booking service. Include a header with authentication credentials and a body with departure city, arrival city, and date.

FAQ

Can a SOAP message have multiple headers?

: Yes. Multiple header blocks can be included for different purposes.

Is the SOAP Envelope always required?

: Yes. The Envelope is mandatory for all SOAP messages.

Can SOAP messages be sent asynchronously?

: Yes, through transports like JMS or using WS-Addressing for asynchronous patterns.

What's Next

Learn about the SOAP Envelope in detail, then explore SOAP Header elements and WS-Addressing.

Built by the developers of DodaTech

Doda Browser, DodaZIP & Durga Antivirus Pro