<Fruits>
<Fruit>
<Family>Citrus</Family>
<Explanation>this is a Citrus fruit.</Explanation>
<Type>Orange</Type>
<Type>Lemon</Type>
</Fruit>
</Fruits>
I want to extract the Explanation of this XML code and assign it to both fruits(Type) next to them. This is my code:
import os
from xml.etree import ElementTree
file_name = "example.xml"
full_file = os.path.abspath(os.path.join("xml", file_name))
dom = ElementTree.parse(full_file)
Fruit = dom.findall("Fruit")
for f in Fruit:
Type = f.find("Type").text
Explanation = f.find("Explanation").text
print (Type, Explanation)
I am just getting the result for the first fruit of the tree structure.
Orange, this is a Citrus fruit.
But I would like to get all types with their explanation assigned next to it. Therefore the result should look like this:
Orange, this is a Citrus fruit.
Lemon, this is a Citrus fruit.