XPath query in XML using Python

Is it possible to use XPath Query in Python when processing XML. I am using minidom which does not support this. Is there any other module for this?

+6
python
source share
3 answers

http://docs.python.org/library/xml.etree.elementtree.html

etree supports XPath queries, just like lxml.

etree is included in the standard library, but lxml is faster.

+7
source share

My favorite Python XML processing library is lxml , which, since it is a wrapper around libxml2, also supports full XPath.

There is also 4Suite , which is a cleaner Python solution.

+2
source share

ElementTree is included. Under 2.6 and below, its xpath is rather weak, but in 2.7 it has improved significantly :

import xml.etree.ElementTree as et root = et.parse(filename) result = '' # How to make decisions based on attributes even in 2.6 for e in root.findall('.//child/grandchild'): if e.attrib.get('name') == 'foo': result = e.text break 
+1
source share

All Articles