Confused about using XPath or not
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 shoul开发者_JAVA百科d when I have a choose and why?
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.
精彩评论