473,671 Members | 2,261 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Creating referenceable objects from XML

Hi All,

I'm looking for a quality Python XML implementation. All of the DOM
and SAX implementations I've come across so far are rather
convoluted. Are there any quality implementations that will (after
parsing the XML) return an object that is accessible by name? Such as
the following:


xml = """
<book>
<title>MyBook </title>
<author>the author</author>
</book>
"""

And after parsing the XML allow me to access it as so:

book.title

I need it to somehow convert my XML to intuitively referenceable
object. Any ideas? I could even do it myself if I knew the
mechanism by which python classes do this (create variables on the fly).

Thanks in advance!
Dec 5 '05 #1
7 1592

Michael Williams wrote:
Hi All,

I'm looking for a quality Python XML implementation. All of the DOM
and SAX implementations I've come across so far are rather
convoluted. Are there any quality implementations that will (after
parsing the XML) return an object that is accessible by name? Such as
the following:
xml = """
<book>
<title>MyBook </title>
<author>the author</author>
</book>
"""

And after parsing the XML allow me to access it as so:

book.title

I need it to somehow convert my XML to intuitively referenceable
object. Any ideas? I could even do it myself if I knew the
mechanism by which python classes do this (create variables on the fly).

Thanks in advance!


You might want to take a look at Fredrik Lundh's ElementTree
(and cElementTree) modules:
http://effbot.org/zone/element-index.htm

Dec 5 '05 #2
Michael Williams wrote:
I'm looking for a quality Python XML implementation. All of the DOM
and SAX implementations I've come across so far are rather
convoluted.
Welcome to the wonderful world of XML.
I need it to somehow convert my XML to intuitively referenceable
object. Any ideas? I could even do it myself if I knew the
mechanism by which python classes do this (create variables on the fly).


You've been given the advice to use ElementTree - I can only second
that.

But if for whatever reason you do want to do it yourself (or for future
use), the

getattr/setattr

functions are what you are looking for. Look them up in TFM.

Regards,

Diez

Dec 5 '05 #3

Michael Williams wrote:
Hi All,

I'm looking for a quality Python XML implementation. All of the DOM
and SAX implementations I've come across so far are rather
convoluted. Are there any quality implementations that will (after
parsing the XML) return an object that is accessible by name? Such as
the following:


xml = """
<book>
<title>MyBook </title>
<author>the author</author>
</book>
"""

And after parsing the XML allow me to access it as so:

book.title

I need it to somehow convert my XML to intuitively referenceable
object. Any ideas? I could even do it myself if I knew the
mechanism by which python classes do this (create variables on the fly).

Thanks in advance!


Michael

Here's an approach to ElementTree that worked for me. It's not generic
or anything and a bit brittle (eg. it won't handle missing nodes) but
maybe for a simple, flat schema or for a prototype?

All the best

Gerard

(TOY CODE - NOT TESTED MUCH)

from elementtree import ElementTree

class ElementWrapper( object):

def __tostring(self ):
return ElementTree.tos tring(self.elem ent)

def __fromstring(se lf, xml):
self.element = ElementTree.fro mstring(xml)

xml = property( __tostring, __fromstring )

def __init__(self, element=None):
self.element = element

def __str__(self):
return self.xml

def parse(self, infile):
tree = ElementTree.par se(infile)
self.element = tree.getroot()

def write(self, outfile):
ElementTree.Ele mentTree(self.e lement).write(o utfile)

###########
from elementtree.Ele mentTree import Element
from elementwrapper import ElementWrapper

xmlns = 'http://schemas/email/0.1'
class MailDocument(El ementWrapper):

def __build_element (self):
root = Element('{%s}Ma il' % xmlns)
root.append( Element('{%s}Da te' % xmlns) )
root.append( Element('{%s}Fr om' % xmlns) )
root.append( Element('{%s}Su bject' % xmlns) )
root.append( Element('{%s}To ' % xmlns) )
root.append( Element('{%s}Cc ' % xmlns) )
root.append( Element('{%s}Bo dy' % xmlns) )
root.append( Element('{%s}At tachments' % xmlns) )
self.element = root

############### ############### ############### ########
# Properties
#
def __get_uid(self) :
return self.element.ge t('id')

def __set_uid(self, id=''):
self.element.se t('id', id)

def __get_date(self ):
return self.element[0].text

def __set_date(self , value=''):
self.element[0].text = value

def __get_from(self ):
addr = self.element[1].get('address')
nm = self.element[1].get('name')
return addr, nm

def __get_subject(s elf):
return self.element[2].text

def __set_subject(s elf, value=''):
self.element[2].text = value

def __get_body(self ):
return self.element[5].text

def __set_body(self , value=''):
self.element[5].text = value

uid = property( __get_uid, __set_uid )
From = property( __get_from)
subject = property( __get_subject, __set_subject )
date = property( __get_date, __set_date )
body = property( __get_body, __set_body )

def set_from_header (self, address='', name=''):
self.element[1].set('address', address)
self.element[1].set('name', name)
#
# End Properties
############### ############### ############### ########

############### ############### ############### ########
# Lists
#
def add_to_header(s elf, address='', name=''):
self.__add_mail to( self.element[3], address, name )

def remove_to_heade r(self, index):
elem = self.element[3][index]
self.element[3].remove(elem)

def add_cc_header(s elf, address='', name=''):
self.__add_mail to( self.element[4], address, name )

def remove_cc_heade r(self, index):
elem = self.element[4][index]
self.element[4].remove(elem)

def add_attachment( self, filename='', fileuri='', filetype=''):
elem = Element("{%s}Ur i" % xmlns, value=fileuri, type=filetype
)
elem.text = filename
self.element[6].append( elem )

def remove_attachme nt(self, index):
elem = self.element[6][index]
self.element[6].remove(elem)

def __add_mailto(se lf, element, Address='', Name=''):
element.append( Element("{%s}ma ilto" % xmlns, address=Address ,
name=Name ) )

def get_to_headers( self):
hdrs = []
for item in self.element[3]:
hdrs.append( ( item.get('addre ss'), item.get('name' ) ) )
return hdrs

def get_cc_headers( self):
hdrs = []
for item in self.element[4]:
hdrs.append( (item.get('addr ess'), item.get('name' ) ) )
return hdrs

def get_attachments (self):
ret = []
for item in self.__element[6]:
hdrs.append( (item.text, item.get('value '),
item.get('type' ) ) )
return hdrs
#
# End Lists
############### ############### ############### ###########

############### ############### ############### ###########
# Initialise
#
def __init__(self):
self.__build_el ement()
self.__set_uid( )
self.__set_date ()
self.__set_subj ect()
self.set_from_h eader()
self.__set_body ()
#
# End Initialise
############### ############### ############### ###########

xml_test ='''
<mail:Mail xmlns:mail="htt p://schemas/email/0.1">
<mail:Date>10/10/05</mail:Date>
<mail:From ad*********@org .org' name='Mr. Jones'/>
<mail:Subject>j ust a note</mail:Subject>
<mail:To>
<mail:mailto ad**********@or g.org' name='Mrs Jones' />
<mail:mailto ad************* **@org.org' name='Alan Nother' />
</mail:To>
<mail:Cc></mail:Cc>
<mail:Body>hi there,
just a note to say hi there!</mail:Body>
<mail:Attachmen ts></mail:Attachment s>
</mail:Mail>
'''
if __name__ == '__main__':
mail = MailDocument()
mail.xml = xml_test
#mail.parse('te st/data/test.xml')
print 'From: ' + mail.From[0]
print 'Subject: ' + mail.subject
mail.set_from_h eader('n**@new. com')
print 'From: ' + mail.From[0]
mail.add_to_hea der('aaa.bbb@cc c', 'aaaaaa')
mail.add_to_hea der('fff.ggg@hh h', 'ffffff')
print 'To:'
for hdr in mail.get_to_hea ders():
print hdr
mail.remove_to_ header(1)
print 'To:'
for hdr in mail.get_to_hea ders():
print hdr
#mail.write('te st_copy.xml')

Dec 5 '05 #4
Michael Williams wrote:
Hi All,

I'm looking for a quality Python XML implementation. All of the DOM
and SAX implementations I've come across so far are rather convoluted.
Are there any quality implementations that will (after parsing the XML)
return an object that is accessible by name? Such as the following:


xml = """
<book>
<title>MyBook </title>
<author>the author</author>
</book>
"""

And after parsing the XML allow me to access it as so:

book.title

I need it to somehow convert my XML to intuitively referenceable
object. Any ideas? I could even do it myself if I knew the mechanism
by which python classes do this (create variables on the fly).

Thanks in advance!


Another tool (ElementsTree already quoted): Amara
( http://uche.ogbuji.net/uche.ogbuji.n.../4suite/amara/ )

[never tested but bookmarked as it seem interresting]

A+

Laurent.
Dec 5 '05 #5
Michael Williams wrote:
Hi All, I'm looking for a quality Python XML implementation. All of the DOM
and SAX implementations I've come across so far are rather
convoluted. Are there any quality implementations that will (after
parsing the XML) return an object that is accessible by name? Such as
the following: xml = """
<book>
<title>MyBook </title>
<author>the author</author>
</book>
""" And after parsing the XML allow me to access it as so: book.title I need it to somehow convert my XML to intuitively referenceable
object. Any ideas? I could even do it myself if I knew the
mechanism by which python classes do this (create variables on the fly).


Looks as if MIchael is working with Amara now, but I did want to note
for the record that APIs that allow one to access a node in the
"book.title " fashion are what I call Python data bindings.

Python data bindings I usually point out are:

Amara Bindery: http://www.xml.com/pub/a/2005/01/19/amara.html
Gnosis: http://www.xml.com/pub/a/2003/07/02/py-xml.html
generateDS: http://www.xml.com/pub/a/2003/06/11/py-xml.html

Based on updates to EaseXML in response to my article another entry
might be:

EaseXML: http://www.xml.com/pub/a/2005/07/27/py-xml.html

ElementTree ( http://www.xml.com/pub/a/2003/02/12/py-xml.html ) is a
Python InfoSet rather than a Python data binding. You access nodes
using generic names related to the node type rather than the node name.
Whether data bindings or Infosets are your preference is a matter of
taste, but it's a useful distinction to make between the approaches.
It looks as if Gerald Flanagan has constructed a little specialized
binding tool on top of ElementTree, and that's one possible hybrid
approach.

xmltramp ( http://www.aaronsw.com/2002/xmltramp/ ) is another
interesting hybrid.

--
Uche Ogbuji Fourthought, Inc.
http://uche.ogbuji.net http://fourthought.com
http://copia.ogbuji.net http://4Suite.org
Articles: http://uche.ogbuji.net/tech/publications/

Dec 11 '05 #6
[Michael Williams]
I need it to somehow convert my XML to intuitively referenceable
object. Any ideas? I could even do it myself if I knew the mechanism
by which python classes do this (create variables on the fly).


You seem to already have a fair idea what kind of model you need, and to
know that there is a simple way for you to create one. I encourage you
to progress on this path: it will increase the depth of your understanding.

One mistake I think that some people make about XML is relying on other
peoples interpretations of the subject, rather than forming their own
opinions.

The multitude of document models provided by everyone and his mother all
make assumptions about how the components of the model will be accessed,
in what order those components will be accessed, how often and when, how
memory efficient the model is, etc, etc.

To really understand the trade-offs and strengths of all the different
models, it is a good exercise to build your own object model. It's a
simple exercise, due to pythons highly dynamic nature. Understanding
your own model will help you understand what the other models do and do
not provide. You can then evaluate other off-the-shelf models for your
specific applications: I always find different XML tools suit different
situations.

See this post of mine from a couple years back about different ways of
building your own document/data models.

http://groups.google.com/group/comp....a4a1c35395ffec

I think the reference to the ActiveState recipe will be of particular
interest, since you could have a running example very quickly indeed.

See also my tutorial post on extracting document content from a SAX
stream. I gave the example of a simple stack-based xpath-style
expression matcher.

http://groups.google.com/group/comp....53bddbb9326948

Also contained in that thread is an illuminating and productive
discussion between the effbot and myself about how wonderfully simple
ElementTree makes this, not to mention unbeatably efficient.

this-week-i-ave-been-mostly-using-kid-for-templating-ly'yrs,

--
alan kennedy
------------------------------------------------------
email alan: http://xhaus.com/contact/alan
Dec 11 '05 #7
uc*********@gma il.com wrote:
ElementTree ( http://www.xml.com/pub/a/2003/02/12/py-xml.html ) is a
Python InfoSet rather than a Python data binding. You access nodes
using generic names related to the node type rather than the node name.
Whether data bindings or Infosets are your preference is a matter of
taste, but it's a useful distinction to make between the approaches.
It looks as if Gerald Flanagan has constructed a little specialized
binding tool on top of ElementTree, and that's one possible hybrid
approach.


in my experience, it's hard to make a python/xml mapping that's well suited for all
possible use cases (many bindings suffer from issues with namespaces, collisions
between tags/attribute names and python names, etc), but it's usually trivial to write
a custom wrapper for a specific case.

for most normal use, manual infoset navigation is often the easiest way to pull out
data from the infoset (find, get, findtext, int, float, etc).

for certain cases, creating wrappers on demand can be quite efficient; e.g.

http://online.effbot.org/2003_07_01_...element-tricks

and for highly regular cases, incremental parsing/conversion up front is often the
fastest and most efficient way to deal with data; e.g.

http://effbot.org/zone/element-iterparse.htm#plist

</F>

Dec 12 '05 #8

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

Similar topics

0
4069
by: Norberto Eichstaedt | last post by:
Hi, I have a XML file that uses "html:table". <book> <chapter> <html:table> <html:tbody> .....
8
5961
by: Nanda | last post by:
hi, I am trying to generate parameters for the updatecommand at runtime. this.oleDbDeleteCommand1.CommandText=cmdtext; this.oleDbDeleteCommand1.Connection =this.oleDbConnection1; this.oleDbDeleteCommand1.Parameters.Add(new System.Data.OleDb.OleDbParameter("Original_ApplicantName", dataset.Tables.Columns.DataType, 50,
2
1531
by: JJ L. | last post by:
Hello. I have a project that consists of nine different objects, each serving their own purpose. In the past I have just created a form for each one, and then whenever you call, say, object.Display(), it would call up the form associated with that object. This form only displays information, it doesn't allow the user to edit any information. Is it possible to find the properties of a certain object, and then loop through creating labels...
1
232
by: Chris | last post by:
Hi, to create an array of 2 objects (e.g. of type '__gc class Airplane') I need to do : Airplane * arrAirplanes __gc = new Airplane* __gc; arrAirplanes = new Airplane("N12344"); arrAirplanes = new Airplane("N12345"); Actually, I create an array of Airplane-pointers first and then create the
3
1859
by: Ken Varn | last post by:
I am just starting the process of creating ASP.NET server controls. I have created controls for .NET applications, but have just started with ASP.NET. I am a little confused about some areas that I am hoping someone can help clear up. 1. What is the difference between initializing a control in the constructor, vs the OnInit(), vs the CreateChildControls() methods? 2. The control that I created contains an Items collection attribute...
5
2275
by: | last post by:
Trying to learn about manipulating collections of objects, and populating these objects dynamically from datasources. Could someone post a code sample that shows the following: Instantiating a collection object -- say, a dictionary. Populating that collection object with custom objects, say, Person. What I really want to see is how to populate the properties of those Person objects from a datasource: instantiate one Person, fill...
1
1352
by: chris | last post by:
I know I've asked this before, but I didn't really get an answer and I bet it's because I didn't explain myself very well. Here goes again. I have this code: Dim arrData(intNoOfRows, intNoOfColumns) As Object Dim intR As Integer For intC As Integer = 0 To intNoOfColumns - 1
5
1541
by: fireball | last post by:
please help newbie I need to create a lot of objects the same type (let's say: schemas) I wish to use paramerized block in loop to do so. - how to put names of my objects to such control-flow? belss you for help
31
3182
by: JoeC | last post by:
I have read books and have ideas on how to create objects. I often create my own projects and programs. They end up getting pretty complex and long. I often use objects in my programs they are some of the most powerful programming tools I have found. Often times as my program grows so do my objects. Often times I look back and see that my objects could be broken down int several smaller more re-usable module pieces of code. Is it a...
0
8393
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8917
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8821
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
8598
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
8670
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6229
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4225
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2812
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1809
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.