|
Project Information
Members
Featured
Downloads
Wiki pages
|
What is Elements
Elements is a Python library that provides simple parsing from XML to Python objects and back. It uses the well known ElementTree API to parse XML into Python objects. ElementTrees are very nice for parsing XML, but sometimes you want something even closer to Python. You want the XML to "magically" fill out your Python objects, and be able to access your values using dotted notation. This is what Elements does for you. Elements is similar to lxml.objectify and Amara2 Bindery. However, Elements does not work well for all kinds of XML documents. It's a compromise. It's very good for parsing XML over HTTP based protocols such as Atom, but may not work well for you when the ordering of the elements is significant such as for XHTML or Apple Property Lists (PList). At first you might get the feeling of over-engineering when looking at Elements, but once your classes are defined, you will feel and enjoy the power of the this library. HistoryMost of the idea, design and code has been taken from the CouchDB Python Library. They have made a fantastic implementation of their Schema/Mapping which is a higher-level API for mapping between CouchDB documents and Python objects. With Elements v2 we are using the same design to parse and generate XML documents. Elements v1 was based on the Atom parser of the gdata-python-client library. I really liked the idea and wanted to make it easy to use with other applications and for other XML formats as well. Elements v1 works well but the class declarations are quite verbose. SourceTo browse the Elements library source code, visit the Source tab. ExampleA simple example. Lets say you want to parse the following XML document: <Example version="1.0">
<Name>Dag Brattli</Name>
<Age>39</Age>
<Hacker/>
</Example>With Elements you have to declare a Python class for storing the information you are interested in. Unspecified elements and attributes will be ignored by the parser. from elements import Element
class Example(Element):
_tag = 'Example'
version = TextAttribute()
hacker = BoolElement(tag="Hacker")
name = TextElement(name="Name")
age = IntegerElement(name="Age")) Now you are ready for some action: >>> e = Example.from_string(example) # The XML doc above >>> print e.name Dag Brattli >>> print e.age 39 >>> print e.hacker True Nice! There are many other and more advanced features, so check out the documentation for details. Enjoy Dag Brattli |