Skip to content
Advertisement

How do I save an Element Tree to a list based on an attribute in a child tag using Python’s LXML module?

I have an xml document that I have to parse. I’m using python 3.8 and the lxml module.

The XML contains Titles which has other child element tags like the xml below. I need to only find the “change” events and keep that “Title” in a list. I would like to save all of the tags of that title, so I can extract the data that I need.

Here is my XML example:

JavaScript

I’ve tried using the findall() function, but it only seems to keep the “Event” tag not the “Title” tag and all of its children. I get the same results when using xpath too.

Advertisement

Answer

If txt is your XML snippet from the question, then you can do this to extract <Title> tags which contain <Event type="change">:

JavaScript

Prints:

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