Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Python object <-> XML

Reply
Thread Tools

Python object <-> XML

 
 
Samuel
Guest
Posts: n/a
 
      09-03-2007
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

 
Reply With Quote
 
 
 
 
Jarek Zgoda
Guest
Posts: n/a
 
      09-03-2007
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.

--
Jarek Zgoda
http://jpa.berlios.de/
 
Reply With Quote
 
 
 
 
Samuel
Guest
Posts: n/a
 
      09-03-2007
On Sep 3, 10:19 pm, Jarek Zgoda <(E-Mail Removed)> 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

 
Reply With Quote
 
Laurent Pointal
Guest
Posts: n/a
 
      09-03-2007
Samuel wrote:

> 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/e...l.html#bindery

A+

Laurent.

 
Reply With Quote
 
Stefan Behnel
Guest
Posts: n/a
 
      09-04-2007
Samuel wrote:
> 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....ect-attributes
http://codespeak.net/lxml/objectify....hon-data-types
http://codespeak.net/lxml/objectify....l-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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Different results parsing a XML file with XML::Simple (XML::Sax vs. XML::Parser) Erik Wasser Perl Misc 5 03-05-2006 10:09 PM
Object creation - Do we really need to create a parent for a derieved object - can't the base object just point to an already created base object jon wayne C++ 9 09-22-2005 02:06 AM
Transform Java object to XML string object Matt Java 7 05-19-2004 05:20 AM
Dropdown list + XML = Object reference not set to an instance of an object. darrel ASP .Net 0 04-27-2004 03:42 PM
Help! Web Service + XML Object Representatino vs. Real Object Arthur Mnev ASP .Net 0 02-02-2004 06:14 AM



Advertisments