XML Services

Web services using XML for data exchange

🌐 What are XML Services?

XML services are web services that use XML format to exchange data between applications over the internet. They enable different systems to communicate regardless of platform or programming language.


<!-- XML Service Request -->
<request>
  <method>getUser</method>
  <userId>123</userId>
</request>
                                    

XML Service Types

🔌

SOAP Services

Protocol-based XML web services

<soap:Envelope>
  <soap:Body>...</soap:Body>
</soap:Envelope>
🔗

REST Services

Lightweight XML data exchange

<response>
  <status>success</status>
</response>
📡

XML-RPC

Remote procedure calls using XML

<methodCall>
  <methodName>getData</methodName>
</methodCall>
📋

RSS/Atom Feeds

XML-based content syndication

<rss version="2.0">
  <channel>...</channel>
</rss>

🔹 Basic XML Service Request

A simple XML request to retrieve user information from a web service. The client sends structured XML data and receives an XML response.

<!-- Request XML -->
<?xml version="1.0" encoding="UTF-8"?>
<serviceRequest>
  <operation>getUserInfo</operation>
  <parameters>
    <userId>12345</userId>
  </parameters>
</serviceRequest>

<!-- Response XML -->
<?xml version="1.0" encoding="UTF-8"?>
<serviceResponse>
  <status>success</status>
  <data>
    <user>
      <id>12345</id>
      <name>John Doe</name>
      <email>[email protected]</email>
    </user>
  </data>
</serviceResponse>

Result:

✅ Request sent to service endpoint

✅ Service processes request and returns user data

✅ Client receives structured XML response

🔹 RESTful XML Service

REST services use HTTP methods with XML for data exchange. They are simpler than SOAP and widely used for web APIs and mobile applications.

<!-- GET Request: Retrieve product -->
GET /api/products/101 HTTP/1.1
Accept: application/xml

<!-- XML Response -->
<?xml version="1.0"?>
<product>
  <id>101</id>
  <name>Laptop</name>
  <price>899.99</price>
  <inStock>true</inStock>
</product>

<!-- POST Request: Create new product -->
POST /api/products HTTP/1.1
Content-Type: application/xml

<?xml version="1.0"?>
<product>
  <name>Mouse</name>
  <price>29.99</price>
  <inStock>true</inStock>
</product>

<!-- Response -->
<?xml version="1.0"?>
<response>
  <status>created</status>
  <productId>102</productId>
  <message>Product created successfully</message>
</response>

REST HTTP Methods:

  • GET: Retrieve data
  • POST: Create new resource
  • PUT: Update existing resource
  • DELETE: Remove resource

🔹 XML-RPC Service

XML-RPC allows remote procedure calls using XML over HTTP. It's a simple protocol for calling functions on remote servers with parameters and return values.

<!-- XML-RPC Request -->
<?xml version="1.0"?>
<methodCall>
  <methodName>calculateSum</methodName>
  <params>
    <param>
      <value><int>5</int></value>
    </param>
    <param>
      <value><int>10</int></value>
    </param>
  </params>
</methodCall>

<!-- XML-RPC Response -->
<?xml version="1.0"?>
<methodResponse>
  <params>
    <param>
      <value><int>15</int></value>
    </param>
  </params>
</methodResponse>

Result:

📤 Client calls calculateSum(5, 10)

⚙️ Server processes the calculation

📥 Server returns result: 15

🔹 RSS Feed Example

RSS feeds use XML to syndicate content like blog posts and news articles. Readers can subscribe to feeds and receive automatic updates.

<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>Tech News Blog</title>
    <link>https://example.com</link>
    <description>Latest technology news and updates</description>
    <language>en-us</language>
    
    <item>
      <title>New XML Tutorial Released</title>
      <link>https://example.com/xml-tutorial</link>
      <description>Learn XML from scratch with examples</description>
      <pubDate>Wed, 16 Oct 2024 10:00:00 GMT</pubDate>
      <guid>https://example.com/xml-tutorial</guid>
    </item>
    
    <item>
      <title>Understanding Web Services</title>
      <link>https://example.com/web-services</link>
      <description>A guide to SOAP and REST services</description>
      <pubDate>Tue, 15 Oct 2024 14:30:00 GMT</pubDate>
      <guid>https://example.com/web-services</guid>
    </item>
    
  </channel>
</rss>

🔹 Error Handling in XML Services

Proper error responses help clients understand what went wrong and how to fix it:

<!-- Error Response -->
<?xml version="1.0"?>
<serviceResponse>
  <status>error</status>
  <error>
    <code>404</code>
    <message>User not found</message>
    <details>No user exists with ID 99999</details>
  </error>
</serviceResponse>

<!-- Validation Error -->
<?xml version="1.0"?>
<serviceResponse>
  <status>error</status>
  <error>
    <code>400</code>
    <message>Invalid input</message>
    <details>Email format is invalid</details>
  </error>
</serviceResponse>

Common HTTP Status Codes:

  • 200: Success
  • 201: Created
  • 400: Bad Request
  • 404: Not Found
  • 500: Server Error

🔹 Complete Service Example

A complete weather service with request and response:

<!-- Weather Service Request -->
<?xml version="1.0"?>
<weatherRequest>
  <location>
    <city>New York</city>
    <country>USA</country>
  </location>
  <units>metric</units>
</weatherRequest>

<!-- Weather Service Response -->
<?xml version="1.0"?>
<weatherResponse>
  <status>success</status>
  <location>
    <city>New York</city>
    <country>USA</country>
  </location>
  <current>
    <temperature>22</temperature>
    <condition>Partly Cloudy</condition>
    <humidity>65</humidity>
    <windSpeed>15</windSpeed>
  </current>
  <forecast>
    <day>
      <date>2024-10-17</date>
      <high>25</high>
      <low>18</low>
      <condition>Sunny</condition>
    </day>
  </forecast>
</weatherResponse>

🧠 Test Your Knowledge

Which HTTP method is used to retrieve data in REST services?