[XML-SIG] xmlpickle.py ?!

Andy Robinson andy@reportlab.com
Wed, 9 Aug 2000 04:24:07 -0700 (PDT)

I have a related requirement to Marc-Andre's, which is
a need to map XML to (possibly pre-existing) Python
object hierarchies and to slurp stuff in quickly.  
DOM helps, but even after making a DOM tree I still
need to walk through it establishing the mapping.

I've been considering a two-stage approach.  The first
stage is a bit like Greg Stein's qp_xml; it uses
pyexpat, a Parser class whch constructs a tree, and a
base Node class.  However, you may pass in a "class
map" to the parser.  Thus, when it hits an <Invoice>
tag, it looks for a class "Invoice" in the class map,
then calls various methods on that class:

  xmlSetAttr(self, attr)
  xmlAddChild(self, childNode)
  xmlAddData(self, data)

If the corresponding class is not found, it uses a
plain old Node instance.  This should let you push
down the knowledge of how an attribute or a child is
to be interpreted into the Python class itself.  

For output, Node provides...

  xmlWrite(self, output)

...which by default will write out attributes in
__dict__, but can easily be overridden.    (We could
be more finegrained and have xmlGetAttrs(self) -> dict
and xmlGetContent(self)  -> list of elements.  

The method names would be chosen not to conflict with
anything you are likely to use in your own classes, so
it can be used as a mixin.  We could also write a
clever "dump" routine which would do something
sensible with an arbitrary Python object, but would
call the xmlWrite hook if it existed.

Step Two is inspired by breeze (www.breezefactor.com):
use a DTD or schema to generate the Python classes. 
Or perhaps go the other way, and use some Python data
structure to generate a DTD.

The xmlpickle suggestion interests me because I think
we can integrate these approaches.  We have a generic
xmlpickle, but the provision for hooks to let classes
specify how to serialize and deserialize themselves
from XML. 

Does this make sense? Are there any other ideas around
for ways to map XML to Python objects?

Andy Robinson

Andy Robinson
ReportLab, Inc.

Do You Yahoo!?
Kick off your party with Yahoo! Invites.