Python object <-> XML

Discussion in 'Python' started by Samuel, Sep 3, 2007.

  1. Samuel

    Samuel Guest

    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
    Samuel, Sep 3, 2007
    #1
    1. Advertising

  2. Samuel

    Jarek Zgoda Guest

    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/
    Jarek Zgoda, Sep 3, 2007
    #2
    1. Advertising

  3. Samuel

    Samuel Guest

    On Sep 3, 10:19 pm, Jarek Zgoda <> 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
    Samuel, Sep 3, 2007
    #3
  4. 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/etc/amara-manual.html#bindery

    A+

    Laurent.
    Laurent Pointal, Sep 3, 2007
    #4
  5. 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.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
    Stefan Behnel, Sep 4, 2007
    #5
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Arthur Mnev
    Replies:
    0
    Views:
    314
    Arthur Mnev
    Feb 2, 2004
  2. darrel
    Replies:
    0
    Views:
    422
    darrel
    Apr 27, 2004
  3. Matt
    Replies:
    7
    Views:
    18,961
    Andrew Thompson
    May 19, 2004
  4. jon wayne
    Replies:
    9
    Views:
    698
    Jim Langston
    Sep 22, 2005
  5. Erik Wasser
    Replies:
    5
    Views:
    438
    Peter J. Holzer
    Mar 5, 2006
Loading...

Share This Page