473,413 Members | 1,989 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,413 software developers and data experts.

Python object <-> XML

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

Sep 3 '07 #1
4 1879
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/
Sep 3 '07 #2
On Sep 3, 10:19 pm, Jarek Zgoda <jzg...@o2.usun.plwrote:
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

Sep 3 '07 #3
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.

Sep 3 '07 #4
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
Sep 4 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Josh | last post by:
I've been trying to get Tix running with Python on Windows. Since there is no binary distributions of Tix for Windows, it seems the easiest way to install Tix is through IDiscovery's IDEStudio 1.9...
11
by: Maxim Khesin | last post by:
Hi, being recently introduced to the joys of programming in a powerful dynamic language (go snake!) I periodically rethink which parts of C++ I still miss. One thing I really enjoy is the generics...
0
by: Heiko Wundram | last post by:
Hi all, esp. list admins! Why is it that I always get bounces saying my mails have suspicious headers lately when I post to python-list? This only happens when I reply to myself (as I just did,...
6
by: Gonzalo Monzón | last post by:
Hi all! I have been translating some Python custom C extension code into Python, as I need these modules to be portable and run on a PocketPC without the need of compile (for the purpose its a...
1
by: yichun.wei | last post by:
Perl has the ability to do the following: print <<EOF; ...reams of text goes here... EOF Is there a Python equivalent of the above Perl code? This thread has previous discussion on the...
23
by: Python Maniac | last post by:
I am new to Python however I would like some feedback from those who know more about Python than I do at this time. def scrambleLine(line): s = '' for c in line: s += chr(ord(c) | 0x80)...
2
by: Jinx08 | last post by:
I am probably an itermediate user when it comes to flash however I am at a loss with this problem. I have posted numerous flash files to the internet and never had this problem however the key there...
3
by: suganya | last post by:
Hi Some professionals already has developed the project using menu. In my company, they have given me task to clear the error in that. It is a script file named as "menubarAPI4.js" which is kept...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.