[Tutor] A somewhat easier way to parse XML

Danny Yoo dyoo at hkn.eecs.berkeley.edu
Wed Jan 19 08:37:53 CET 2005



On Wed, 19 Jan 2005, Max Noel wrote:

> 	I've just spent the last few hours learning how to use the DOM XML
> API (to be more precise, the one that's in PyXML), instead of revising
> for my exams :p. My conclusion so far: it sucks (and so does SAX because
> I can't see a way to use it for OOP or "recursive" XML trees).

Hi Max,

You are not alone in this restless feeling.

In fact, Uche Ogbuji, one of the lead developers of 4Suite and Amara
(which Kent mentioned earlier), just wrote a blog entry about his
malcontent with the DOM.  Here, these may interest you:

    http://www.oreillynet.com/pub/wlg/6224
    http://www.oreillynet.com/pub/wlg/6225


> In fact, I find it appalling that none of the "standard" XML parsers
> (DOM, SAX) provides an easy way to do that (yeah, I know that's what
> more or less what the shelve module does, but I want a
> language-independent way).

For simple applications, the 'xmlrpclib' has two functions (dumps() and
loads()) that we can use:

    http://www.python.org/doc/lib/node541.html


For example:

###
>>> s = xmlrpclib.dumps(({'hello': 'world'},))
>>> print s
<params>
<param>
<value><struct>
<member>
<name>hello</name>
<value><string>world</string></value>
</member>
</struct></value>
</param>
</params>
>>>
>>>
>>> xmlrpclib.loads(s)
(({'hello': 'world'},), None)
###

A little bit silly, but it does work.  The nice thing about this that
xmlrpc is pretty much a platform-independent standard, so if we're dealing
with simple values like strings, integers, lists, and dictionaries, we're
all set.  It is a bit verbose, though.

Amara looks really interesting, especially because they have tools for
doing data-binding in a Python-friendly way.


Best of wishes to you!



More information about the Tutor mailing list