I need to parse a xml file to extract some data. I only need some elements with certain attributes, here\'s an example of document:
You can use xpath, e.g. root.xpath("//article[@type='news']")
This xpath expression will return a list of all elements with "type" attributes with value "news". You can then iterate over it to do what you want, or pass it wherever.
To get just the text content, you can extend the xpath like so:
root = etree.fromstring("""
some text
some text
some text
""")
print root.xpath("//article[@type='news']/content/text()")
and this will output ['some text', 'some text']
. Or if you just wanted the content elements, it would be "//article[@type='news']/content"
-- and so on.