<?xml version="1.0" ?> <data> <test > <f1 /> </test > <test2 > <test3> <f1 /> </test3> </test2> <f1 /> </data>
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 !!!
lxml. etree supports the simple path syntax of the find, findall and findtext methods on ElementTree and Element, as known from the original ElementTree library (ElementPath).
lxml provides a very simple and powerful API for parsing XML and HTML. It supports one-step parsing as well as step-by-step parsing using an event-driven API (currently only for XML).
Returns a sequence or iterator of all elements in the subtree in document order (depth first pre-order), starting with this element. Can be restricted to find only elements with specific tags, see iter. Deprecated: Note that this method is deprecated as of ElementTree 1.3 and lxml 2.0.
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>]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With