Skip to content
Advertisement

Python lxml find text efficiently

Using python lxml I want to test if a XML document contains EXPERIMENT_TYPE, and if it exists, extract the <VALUE>.

Example:

JavaScript

Is there a faster way than iterating through all elements?

JavaScript

That attempt is also getting messy when I want to extract the <VALUE>.

Advertisement

Answer

Preferably you do this with XPath which is bound to be incredibly fast. My sugestion (tested and working). It will return a (possible empty) list of VALUE elements from which you can extra the text.

PS: do not use “special” words such as all as variable names. Bad practice and may lead to unexpected bugs.

JavaScript

An alternative XPath declaration was provided below by Michael Kay, which is identical to the answer by Martin Honnen.

JavaScript
User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement