tags:

views:

28

answers:

1

Hi all.

This follows my previous questions on using lxml and Python.

I have a question, as to when I have a choice between using the methods provided by the lxml.etree and where I can make use of XPath, what should I use?

For example, to get a list of all the X tags in a XML document, I could either iterate through it using the getiterator() of lxml.etree, or I could write the XPath expression: //x.

There may be many more examples, this is just one. Question is, which should when I have a choose and why?

A: 

XPath is usually preferable to an explicit iteration over elements. XPath is more succinct, and will likely be faster since it is implemented inside the XML engine.

You'd want to use an explicit iteration if there were complex criteria that couldn't be expressed easily (or at all) in XPath, or if you needed to visit all the nodes for some other processing anyway, or if you wanted to get rich debugging output.

Ned Batchelder
Thanks for the answer.
sukhbir
I just did a benchmark test and the methods provided by xml.etree are much faster than XPath. I can paste the code if you are interested :)
sukhbir