Shaghayegh L
Shaghayegh L

Reputation: 1

Python parse XML content of an element when there is a child element

I have an XML file as below:

<?xml version="1.0" encoding="UTF-8"?>
    <data>
        <text>
            I have <num1>two</num1> apples and <num2>four</num2> mangoes
        </text>
    </data>

I want to parse the file and get the whole context of text and its children elements and assign it to variable sentence:

sentence = "I have two apples and four mangoes"

How can I do that using Python ElementTree?

Upvotes: 0

Views: 344

Answers (1)

Kris
Kris

Reputation: 8873

xml = """
<?xml version="1.0" encoding="UTF-8"?>
<data>
    <text>
        I have <num1>two</num1> apples and <num2>four</num2> mangoes
    </text>
</data>
"""
from xml.etree import ElementTree as ET
x_data = ET.fromstring(xml.strip())
all_text = list(x_data.findall(".//text")[0].itertext())
print(" ".join([text.strip() for text in all_text]))

Iterate through the text from the parent node, and process the text as per your need

Upvotes: 1

Related Questions