How to find recursively for a tag of XML using LXML?


<?xml version="1.0" ?>
    <test >
        <f1 />
    </test >
    <test2 >
         <f1 />
    <f1 />

Using lxml is it possible to find recursively for tag ” f1 “? I tried findall method but it works only for immediate children.

I think I should go for BeautifulSoup for this !!!

Asked By: shahjapan



You can use XPath to search recursively:

>>> from lxml import etree
>>> q = etree.fromstring('<xml><hello>a</hello><x><hello>b</hello></x></xml>')
>>> q.findall('hello')     # Tag name, first level only.
[<Element hello at 414a7c8>]
>>> q.findall('.//hello')  # XPath, recursive.
[<Element hello at 414a7c8>, <Element hello at 414a818>]
Answered By: Max Shawabkeh

iterfind() iterates over all Elements that match the path expression

findall() returns a list of matching Elements

find() efficiently returns only the first match

findtext() returns the .text content of the first match

Illustrative Examples:

>>> root = etree.XML("<root><a x='123'>aText<b/><c/><b/></a></root>")
#Find a child of an Element:
>>> print(root.find("b"))
>>> print(root.find("a").tag)
#Find an Element anywhere in the tree:
>>> print(root.find(".//b").tag)
>>> [ b.tag for b in root.iterfind(".//b") ]
['b', 'b']
#Find Elements with a certain attribute:
>>> print(root.findall(".//a[@x]")[0].tag)
>>> print(root.findall(".//a[@y]"))


(This answer is relevant selective selection from the content at this link)

Answered By: codersofthedark
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.