Flavius
Flavius

Reputation: 13816

lxml[.objectify] documentElement tagName

I'm receiving data packets in XML format, each with a specific documentRoot tag, and I'd like to delegate specialized methods to take care of those packets, based on the root tag name. This worked with xml.dom.minidom, something like this:

dom = minidom.parseString(the_data)
root = dom.documentElement
deleg = getattr(self,'elem_' + str(root.tagName))
deleg(dom)

However, I want to simplify the things (in other parts of the code, not here) by using the more pythonic lxml.objectify.

The problem is I don't know how to get "root.tagName" with lxml, preferably strictly lxml.objectify. Any ideas?

Upvotes: 0

Views: 1056

Answers (2)

John Machin
John Machin

Reputation: 82934

With the help of the lxml docs and the dir() built_in, I managed to produce this:

>>> from lxml import objectify
>>> import StringIO
>>> tree = objectify.parse(StringIO.StringIO('<parent><child>Billy</child><child>Bob</child></parent>'))
>>> root = tree.getroot()
>>> root.tag
'parent'
>>> [(foo.tag, foo.text) for foo in root.getchildren()]
[('child', 'Billy'), ('child', 'Bob')]
>>>

Looks like you need something like

deleg = getattr(self,'elem_' + str(root.tag))
deleg(tree)

Upvotes: 3

Uche Ogbuji
Uche Ogbuji

Reputation: 11

FWIW in Amara Bindery you can do something like:

from amara import bindery
doc = bindery.parse(the_data)
top_elem = doc.xml_elements.next()
deleg = getattr(self, 'elem_' + str(top_elem.xml_qname))
deleg(doc)

And you get a Pythonic API as well, e.g.: doc.html.head.title = u"Change HTML document title"

Upvotes: 0

Related Questions