Python object <-> XML

S

Samuel

Hi,

Say you have the following XML:

<item ref="1">
<name>item 1</name>
</item>
<item ref="2">
<name>item 2</name>
</item>
<group>
<item ref="1" />
<item ref="2" />
<name>my group</name>
</group>

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?

-Samuel
 
J

Jarek Zgoda

Samuel napisa³(a):
Say you have the following XML:

<item ref="1">
<name>item 1</name>
</item>
<item ref="2">
<name>item 2</name>
</item>
<group>
<item ref="1" />
<item ref="2" />
<name>my group</name>
</group>

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.
 
S

Samuel

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
 
L

Laurent Pointal

Samuel said:
Hi,

Say you have the following XML:

<item ref="1">
<name>item 1</name>
</item>
<item ref="2">
<name>item 2</name>
</item>
<group>
<item ref="1" />
<item ref="2" />
<name>my group</name>
</group>

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?

-Samuel

I dont know if this suit your needs:

http://uche.ogbuji.net/tech/4suite/amara/

Take a look at the Amara Bindery:

http://uche.ogbuji.net/tech/4suite/etc/amara-manual.html#bindery

A+

Laurent.
 
S

Stefan Behnel

Samuel said:
Say you have the following XML:

<item ref="1">
<name>item 1</name>
</item>
<item ref="2">
<name>item 2</name>
</item>
<group>
<item ref="1" />
<item ref="2" />
<name>my group</name>
</group>

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?

Try lxml.objectify. It doesn't copy your data into other objects, but it gives
you all the freedom to design your own objects as an abstraction of the XML
data. See here:

http://codespeak.net/lxml/objectify.html

especially these sections:

http://codespeak.net/lxml/objectify.html#element-access-through-object-attributes
http://codespeak.net/lxml/objectify.html#python-data-types
http://codespeak.net/lxml/objectify.html#defining-additional-data-classes

It's part of lxml, which makes it plenty fast, highly flexible and gives you
all the XML features you might ever need. :)

Stefan
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,776
Messages
2,569,603
Members
45,189
Latest member
CryptoTaxSoftware

Latest Threads

Top