简体   繁体   中英

How to get all strings from all nested tags of a xml tag with python's lxml.etree library?

I have an xml file in which it is possible that the following occurs:

...
<a><b>This is</b> some text about <c>some</c> issue I have, parsing xml</a>
...

Edit: Let's assume, the tags could be nested more than only level, meaning

<a><b><c>...</c>...</b>...</a>

I came up with this using the python lxml.etree library.

context = etree.iterparse(PATH_TO_XML, dtd_validation=True, events=("end",))
for event, element in context:
    tag = element.tag
    if tag == "a":
        print element.text # is empty :/
        mystring = element.xpath("string()")
        ...

But somehow it goes wrong.

What I want is the whole string

"This is some text about some issue I have, parsing xml"

But I only get an empty string. Any suggestions? Thanks!

This question has been asked many times.

You can use lxml.html.text_content() method.

import lxml.html
t = lxml.html.fromstring("...")
t.text_content()

REF: Filter out HTML tags and resolve entities in python

OR use lxml.etree.strip_tags() method.

REF: In lxml, how do I remove a tag but retain all contents?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM