XML - Managing Data Exchange/RPC
XML - Managing Data Exchange
|
Related Topics
|
Get Involved
|
Previous Chapter | Next Chapter |
← WDDX | JSTL → |
Author: Nathan Slider
Editor: Nathan Slider
UGA Master of Internet Technology Program, 2005
Learning Objectives
editUpon completion of this chapter, you will be able to
- Discuss XML-RPC
- Create XML-RPC Code based on Examples
RPC Defined
editIn order to fully understand XML-RPC, we should fist define RPC. A Remote Procedure Call (RPC) is a protocol that allows a computer program running on one host to cause code to be executed on another host without the programmer needing to explicitly code for this. An RPC is initiated by the caller (client) sending a request message to a remote system (the server) to execute a certain procedure using arguments supplied. A result message is returned to the caller. There are many variations and subtleties in various implementations, resulting in a variety of different (incompatible) RPC protocols.
In order to allow servers to be accessed by differing clients, a number of standardized RPC systems have been created. Most of these use an Interface Description Language (IDL) to allow various platforms to call the RPC. Web services were the first real attempt to implement RPC between platforms. Using Web services a .NET client can call a remote procedure implemented in Java on a Unix server (and vice versa).
Web services use XML as the IDL, and HTTP as the network protocol. The advantage of this system is simplicity and standardization, the IDL is a text file that is widely understood, and HTTP is built into almost all modern operating systems. An example of such an RPC system is XML-RPC.
XML-RPC Defined
editXML-RPC (Extensible Markup Language Remote Procedure Call) is a Remote Procedure Call protocol encoded in XML. It is a very simple protocol, defining only a handful of data types and commands, and the entire description can be printed on two pages of paper. This is in stark contrast to most RPC systems, where the standards documents often run into the thousands of pages and require considerable software support in order to be used.
It was first created by Dave Winer in 1995 with Microsoft. However, Microsoft considered it too simple and started adding functionality. After several rounds of this, the standard was no longer so simple and became what is now SOAP.
"We wanted a clean, extensible format that's very simple. It should be possible for an HTML coder to be able to look at a file containing an XML-RPC procedure call, understand what it's doing, and be able to modify it and have it work on the first or second try... We also wanted it to be an easy to implement protocol that could quickly be adapted to run in other environments or on other operating systems." -xmlrpc.com
Data Types
editData Types Referenced from XML-RPC
Name | Tag Example | Description |
---|---|---|
array |
<array>
<data>
<value><i4>1404</i4></value>
<value><string>Something Here</string></value>
<value><i4>1</i4></value>
</data>
</array>
|
Array of values, storing no keys |
base64 | <base64>eW91IGNhbid0IHJlYWQgdGhpcyE=</base64> | [Base 64]-encoded binary data |
boolean | <boolean>1</boolean> | [Boolean] logical value (0 or 1) |
date/time | <dateTime.iso8601>19980717T14:08:55</dateTime.iso8601> | Date and time |
double | <double>-12.53</double> | Double [precision] floating number |
integer | <i4>42</i4> | Whole number, [integer] |
string | <string>Hello world!</string> | String of characters. Must follow XML encoding. |
struct |
<struct>
<member>
<name>foo</name>
<value><i4>1</i4></value>
</member>
<member>
<name>bar</name>
<value><i4>2</i4></value>
</member>
</struct>
|
Array of values, storing keys |
nil | <nil/> | Discriminated null value; an XML-RPC extension |
Examples
editAn example of a typical XML-RPC request would be:
<?xml version="1.0"?>
<methodCall>
<methodName>examples.getStateName</methodName>
<params>
<param>
<value><i4>41</i4></value>
</param>
</params>
</methodCall>
An example of a typical XML-RPC response would be:
<?xml version="1.0"?>
<methodResponse>
<params>
<param>
<value><string>South Dakota</string></value>
</param>
</params>
</methodResponse>
A typical XML-RPC fault would be:
<?xml version="1.0"?>
<methodResponse>
<fault>
<value>
<struct>
<member>
<name>faultCode</name>
<value><int>4</int></value>
</member>
<member>
<name>faultString</name>
<value><string>Too many parameters.</string></value>
</member>
</struct>
</value>
</fault>
</methodResponse>
A final example, comparing a PHP associative array with an equivalent XML-RPC <struct>. This array:
Array
(
[0] => 'dogs',
[1] => 'cats',
['animals'] => Array(
[0] => FALSE,
[1] => 'little_dogs',
[2] => 'little_cats',
[3] => 5,
[4] => 2.3,
[5] => 1,
),
);
Becomes the following XML-RPC:
<?xml version="1.0" encoding="utf-8"?>
<methodResponse>
<params>
<param>
<value>
<struct>
<member>
<name>0
</name>
<value><string>dogs</string>
</value>
</member>
<member>
<name>1
</name>
<value><string>cats</string>
</value>
</member>
<member>
<name>animals
</name>
<value>
<array>
<data>
<value><boolean>0</boolean>
</value>
<value><string>little_dogs</string>
</value>
<value><string>little_cats</string>
</value>
<value><i4>5</i4>
</value>
<value><double>2.3</double>
</value>
<value><boolean>1</boolean>
</value>
</data>
</array>
</value>
</member>
</struct>
</value>
</param>
</params>
</methodResponse>