Python object <-> XML

Samuel knipknap at gmail.com
Mon Sep 3 16:38:36 EDT 2007


On Sep 3, 10:19 pm, Jarek Zgoda <jzg... at o2.usun.pl> wrote:
> > Is there an easy way (i.e. without writing a sax/dom parser) to load
> > this into a (number of) Python object(s), manipulate the instance, and
> > save the result back to XML?
>
> Yea, use ElementTree and you'd get a bunch of nested lists of very
> simple objects.

Sorry for being unclear. By "load this into a number of Python
objects" I mean, filling already existing objects with data. In other
words:

class Item(object):
  def __init__(self, ref, name):
    self.ref  = ref
    self.name = name

class Group(object):
  def __init__(self, ref, name, item = []):
    self.ref  = ref
    self.name = name
    self.item = item

mapper = Mapper()
objects = mapper.load('data.xml')
print objects['1']
<Item object at 0x324235a>

(Obviously, in my example the mapper could not always know where a
list is required, but an existing mapper will surely have a solution
implemented.)
I guess what I am looking for is pretty much an ORM that also works
with XML.

-Samuel




More information about the Python-list mailing list