
simplexmlapi
NOTE: This project has moved to GitHub: http://github.com/iancmcc/simplexmlapi. This repository will no longer be updated.
simplexmlapi provides an easy way to access XML data. It is pure Python code with no dependencies.
simplexmlapi uses the xml.dom.minidom module to parse XML data, then allows the resulting document to be walked using a dotted name syntax. It also provides a SimpleXmlApi object which comprises mappings of attributes to dotted-name paths.
The SimpleXmlApi object may be subclassed to provide simple APIs for known data structures.
Installation
View the trunk at: http://simplexmlapi.googlecode.com/svn/trunk/
Check out the development version of the code with:
svn checkout http://simplexmlapi.googlecode.com/svn/trunk/ simplexmlapi
And install it:
$ python setup.py install
Packages of simplexmlapi are available from the Cheese Shop (http://pypi.python.org/pypi/simplexmlapi) and via easy_install:
easy_install simplexmlapi
API documentation may be viewed here: http://packages.python.org/simplexmlapi/
Usage
simplexmlapi exposes an API similar to those of the marshal and pickle modules.
An XML document may be loaded into a SimpleXmlApi object and traversed using Python-like dotted names. One may also map attributes of the SimpleXmlApi object to given dotted names.
Each segment of the dotted name will be resolved to the first child element with that tag name. If no such element exists, it will be resolved to an attribute on the current element. If no such attribute exists, an exception will be raised.
Traversing the tree manually -- that is, without using an attribute map -- will return a DotNode or DotNodeAttribute instance. To get the text value of a node or attribute, access the '_'
property or call the getValue() method. When accessing an attribute mapping, the text value is returned automatically; no call to getValue() is necessary.
If multiple matching child elements exist, __0
may be appended to the name, where '0' is the index of the desired element.
In case of tag name/attribute conflicts, attribute resolution may be specified explicitly by appending __a
to the name.
Parsing an XML document and traversing elements with dotted names: ```
import simplexmlapi s = ''' ... ... A Value ... A Thing ... ... ''' api = simplexmlapi.loads(s) api.obj.prop._ u'A Value' api.obj.prop.name._ 'value' api.obj.prop__1.name._ 'thing' api.obj.prop__1.name__a._ 'thing' from StringIO import StringIO io = StringIO(s) api = simplexmlapi.load(io) api.obj.prop._ u'A Value'
Mapping dotted names to attributes:
import simplexmlapi s = ''' ... ... A Value ... A Thing ... ... ''' api = simplexmlapi.loads(s) api.add_mapping('value', 'obj.prop__0') api.add_mapping('thing', 'obj.prop__1') api.value u'A Value' api.thing u'A Thing' attr_map = dict(value="obj.prop__0", thing="obj.prop__1") api2 = simplexmlapi.loads(s, map=attr_map) api2.value u'A Value' api2.thing u'A Thing'Extending SimpleXmlApi:>>> import simplexmlapi class SampleApi(simplexmlapi.SimpleXmlApi): ... _map = { ... 'value' : 'obj.prop__0', ... 'thing' : 'obj.prop__1' } ... s = ''' ... <xml> ... <obj> ... <prop name="value">A Value</prop> ... <prop name="thing">A Thing</prop> ... </obj> ... </xml> ... ''' api = simplexmlapi.loads(s, cls=SampleApi) api.value u'A Value' api.thing u'A Thing'}}}