Developer Forums | About Us | Site Map
Search  
HOME > TUTORIALS > CLIENT SIDE CODING > XML TUTORIALS > XML RPC AS OBJECT MODEL


Sponsors





Useful Lists

Web Host
site hosted by netplex

Online Manuals

XML-RPC as object model
By David Mertz, Ph.D. - 2003-12-11 Page:  1 2 3 4 5 6

Representing objects

Let's create an object, then serialize it using two different approaches. Some contrasts will come to the fore:

Listing 2. Python shell example of XML-RPC serialization
>>> import xmlrpclib

>>> class C: pass
..
>>> c = C()
>>> c.bool, c.int, c.tup = (xmlrpclib.True, 37, (11.2, 'spam') )

>>> print xmlrpclib.dumps((c,),'PyObject')
<?xml version='1.0'
?>
<methodCall>
<methodName>PyObject</methodName>

<params>
<param>
<value><struct>
<member>
<name>tup</name>
<value><array><data>

<value><double>11.2</double></value>
<value><string>spam</string></value>
</data></array></value>
</member>

<member>
<name>bool</name>
<value><boolean>1</boolean></value>
</member>
<member>
<name>int</name>

<value><int>37</int></value>
</member>
</struct></value>
</param>
</params>
</methodCall>

You should note a few things already. First, the whole XML document has a root <methodCall> element which is irrelevant to our current purposes. Other than a few bytes extra, however, the additional enclosing element is unimportant. Likewise, the <methodName> is superfluous, but the example gives a name that indicates the role of the document. Moreover, a call to xmlrpclib.dumps() accepts a tuple of objects, but we are only interested in "pickling" one (if there were others, they would have their own <param> element). But other than some wrapping, the attributes of our object are well-contained within the <struct> element's <member> elements.

Now let's look at what xml_pickle does (the object is the same as above):

Listing 3. Python shell example of XML-RPC serialization
>>> from xml_pickle import XML_Pickler

>>> print XML_Pickler(c).dumps()
<?xml version="1.0"
?>
<!DOCTYPE PyObject SYSTEM "PyObjects.dtd">
<PyObject class="C" id="1840428">

<attr name="bool" type="PyObject"
class="Boolean" id="1320396">
  <attr name="value" type="numeric" value="1" />

</attr>
<attr name="int" type="numeric" value="37" />
<attr name="tup" type="tuple" id="1130924">

  <item type="numeric" value="11.199999999999999" />
  <item type="string" value="spam" />

</attr>
</PyObject>

There is both less and more to the xml_pickle version (the actual sizes of both are comparable). Notice that even though Python does not have a built-in Boolean type, when you use a class to represent a new type, xml_pickle adjusts readily (albeit more verbosely). XML-RPC, by contrast, is limited to serializing its eight data types, and nothing else. Of course, two of those types,<array> and <struct>, are themselves collections and can be compound. In addition, xml_pickle can point multiple collection members to the same underlying object; this is absent by design from XML-RPC (and introduced in later versions of xml_pickle also). As a small matter, xml_pickle contains only a single numeric type attribute, but the actual pattern of the value attribute allows for decoding to integer, float, complex, and so on. No real generality is lost or gained by these strategies, although the XML-RPC style will appeal aesthetically to programmers working with statically typed languages.



View XML-RPC as object model Discussion

Page:  1 2 3 4 5 6 Next Page: Weaknesses of XML-RPC

First published by IBM developerWorks


Copyright 2004-2017 GrindingGears.com. All rights reserved.
Article copyright and all rights retained by the author.