Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

<?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 !!!

like image 492
shahjapan Avatar asked Apr 27 '10 16:04

shahjapan


People also ask

What is Xpath in lxml?

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).

Can lxml parse HTML?

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).

What is lxml Etree _element?

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.


1 Answers

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>] 
like image 64
Max Shawabkeh Avatar answered Sep 28 '22 19:09

Max Shawabkeh