Getting elements and text with lxml

J. Pablo Fernández pupeno at pupeno.com
Sat May 17 13:43:33 EDT 2008


On May 17, 4:17 pm, Stefan Behnel <stefan... at behnel.de> wrote:
> J. Pablo Fernández wrote:
> > I have an XML file that starts with:
>
> > <vortaro>
> > <art mrk="$Id: a.xml,v 1.10 2007/09/11 16:30:20 revo Exp $">
> > <kap>
> >   <ofc>*</ofc>-<rad>a</rad>
> > </kap>
>
> > out of it, I'd like to extract something like (I'm just showing one
> > structure, any structure as long as all data is there is fine):
>
> > [("ofc", "*"), "-", ("rad", "a")]
>
>     >>> root = etree.fromstring(xml)
>     >>> l = []
>     >>> for el in root.iter():    # or root.getiterator()
>     ...     l.append((el, el.text))
>     ...     l.append(el.text)
>
> or maybe this is enough:
>
>     list(root.itertext())
>
> Stefan

Hello,

My object doesn't have iter() or itertext(), it only has:
iterancestors, iterchildren, iterdescendants, itersiblings.

Thanks.



More information about the Python-list mailing list