Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

python: xml.etree.ElementTree, removing "namespaces"

Tags:

python

xml

I like the way ElementTree parses xml, in particular the Xpath feature. I've an output in xml from an application with nested tags.

I'd like to access this tags by name without specifying the namespace, is it possible? For example:

root.findall("/molpro/job")

instead of:

root.findall("{http://www.molpro.net/schema/molpro2006}molpro/{http://www.molpro.net/schema/molpro2006}job")
like image 421
pygabriel Avatar asked Sep 09 '25 10:09

pygabriel


1 Answers

At least with lxml2, it's possible to reduce this overhead somewhat:

root.findall("/n:molpro/n:job",
             namespaces=dict(n="http://www.molpro.net/schema/molpro2006"))
like image 152
deets Avatar answered Sep 11 '25 11:09

deets