# XML - Managing Data Exchange/RPC

**Author:** Nathan Slider\
**Editor:** Nathan Slider\
UGA Master of Internet Technology
Program, 2005

## Learning Objectives

Upon completion of this chapter, you will be able to

-   Discuss XML-RPC
-   Create XML-RPC Code based on Examples

## RPC Defined

In 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

**XML-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  in 1995 with
. 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
.

\"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

Data Types Referenced from 

+-----------+---------------------------+---------------------------+
| Name      | Tag Example               | Description               |
+===========+===========================+===========================+
| array     | ``` XML                   | Array of values, storing  |
|           |  <array>                  | no keys                   |
|           |    <data>                 |                           |
|           |      <va                  |                           |
|           | lue><i4>1404</i4></value> |                           |
|           |                           |                           |
|           |     <value><string>Someth |                           |
|           | ing Here</string></value> |                           |
|           |                           |                           |
|           | <value><i4>1</i4></value> |                           |
|           |    </data>                |                           |
|           |  </array>                 |                           |
|           | ```                       |                           |
+-----------+---------------------------+---------------------------+
| base64    | `<base64>`{=h             | \[Base 64\]-encoded       |
|           | tml}eW91IGNhbid0IHJlYWQgd | binary data               |
|           | GhpcyE=`</base64>`{=html} |                           |
+-----------+---------------------------+---------------------------+
| boolean   | `<boolean>`{=             | \[Boolean                 |
|           | html}1`</boolean>`{=html} | datatype\|\[Boolean\]     |
|           |                           | logical value (0 or 1)    |
+-----------+---------------------------+---------------------------+
| date/time | \<dateTi                  | Date and time             |
|           | me.iso8601\>19980717T14:0 |                           |
|           | 8:55\</dateTime.iso8601\> |                           |
+-----------+---------------------------+---------------------------+
| double    | `<double>`{=html          | Double \[precision\]      |
|           | }-12.53`</double>`{=html} | floating number           |
+-----------+---------------------------+---------------------------+
| integer   | `<i4                      | Whole number, \[integer\] |
|           | >`{=html}42`</i4>`{=html} |                           |
+-----------+---------------------------+---------------------------+
| string    | `<string>`{=html}Hello    | String of characters.     |
|           | world!`</string>`{=html}  | Must follow               |
|           |                           | XML     |
|           |                           | encoding.                 |
+-----------+---------------------------+---------------------------+
| struct    | ``` XML                   | Array of values, storing  |
|           |  <struct>                 | keys                      |
|           |    <member>               |                           |
|           |      <name>foo</name>     |                           |
|           |                           |                           |
|           | <value><i4>1</i4></value> |                           |
|           |    </member>              |                           |
|           |    <member>               |                           |
|           |      <name>bar</name>     |                           |
|           |                           |                           |
|           | <value><i4>2</i4></value> |                           |
|           |    </member>              |                           |
|           |  </struct>                |                           |
|           | ```                       |                           |
+-----------+---------------------------+---------------------------+
| nil       | `<nil/>`{=html}           | Discriminated null value; |
|           |                           | an XML-RPC                |
|           |                           | ext                      |
|           |                           | ension |
+-----------+---------------------------+---------------------------+

### Examples

An example of a typical XML-RPC request would be:

``` XML
 <?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
 <?xml version="1.0"?>
 <methodResponse>
   <params>
     <param>
         <value><string>South Dakota</string></value>
     </param>
   </params>
 </methodResponse>
```

A typical XML-RPC fault would be:

``` XML
 <?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>`{=html}. This array:

``` PHP
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
<?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>
```

## References

-   XML-RPC Homepage
-   XML-RPC Specification
-   Free Online Dictionary of
    Computing
-   Forum
-   Tutorials
-   Technology Reports
-   Citations from CiteSeer

