Created
May 8, 2009 07:59
-
-
Save svetlyak40wt/108704 to your computer and use it in GitHub Desktop.
Little helper, to access ElementTree nodes as attributes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
######################################################### | |
# Title: Simple ElementTree accessor # | |
# Author: Alexander Artemenko <[email protected]> # | |
# Site: http://aartemenko.com # | |
# License: New BSD License # | |
# Original: http://gist.github.com/108704 # | |
######################################################### | |
class Accessor(object): | |
"""Easy to use ElementTree accessor.""" | |
def __init__(self, xml): | |
self.xml = xml | |
def __repr__(self): | |
return '<Element %s>' % self.xml.tag | |
def __len__(self): | |
return self.xml.__len__() | |
def __iter__(self): | |
return iter(self.xml) | |
def __getattribute__(self, name): | |
""" | |
>>> from xml.etree import ElementTree as ET | |
>>> xml = ET.fromstring('<a b="blah"><c id="1"/><c id="2"><d>Hello</d></c></a>') | |
>>> a = Accessor(xml) | |
>>> a.b | |
'blah' | |
>>> a.c | |
[<Element c>, <Element c>] | |
>>> a.c[1].d | |
<Element d> | |
>>> a.c[1].d.text | |
'Hello' | |
>>> ET.tostring(a) | |
'<a b="blah"><c id="1" /><c id="2"><d>Hello</d></c></a>' | |
""" | |
if name == 'xml': | |
return object.__getattribute__(self, name) | |
self_tag = self.xml.tag | |
if self_tag[0] == '{': | |
el_name = self_tag[:self_tag.rfind('}') + 1] + name | |
else: | |
el_name = name | |
elements = self.xml.findall(el_name) | |
l = len(elements) | |
if l == 1: | |
return Accessor(elements[0]) | |
elif l > 1: | |
return map(Accessor, elements) | |
if self.xml.attrib.has_key(name): | |
return self.xml.attrib[name] | |
if hasattr(self.xml, name): | |
return getattr(self.xml, name) | |
raise AttributeError |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment