For loop: Parsing XML by ElementTree Python3



I designed this simplified XML file that is very similar to the XML file I'll have to parse. I'm trying to parse the text from the following nodes(Food, Date, ItemCondition, Amount)



<?xml version="1.0"?>
<Store>
<Department>
<Produce>
<Food>Apple</Food>
</Produce>
</Department>
<ProductInfo>
<BoxofFruit>
<Qualifiers>
<ItemCondition>New</ItemCondition>
</Qualifiers>
<Date>101214</Date>
<Price>
<LandedPrice>
<CurrencyCode>USD</CurrencyCode>
<Amount>19.99</Amount>
</LandedPrice>
</Price>
</BoxofFruit>
<BoxofFruit>
<Qualifiers>
<ItemCondition>New</ItemCondition>
</Qualifiers>
<Date>091114</Date>
<Price>
<LandedPrice>
<CurrencyCode>USD</CurrencyCode>
<Amount>21.99</Amount>
</LandedPrice>
</Price>
</BoxofFruit>
</ProductInfo>
</Store>


For the life of me I cant seem to get the Food text. Heres my current code that works as long as I dont include the Food portion.



Fruit = root.findall('.//BoxofFruit')
for Apple in Fruit:
Date = Apple.find('Date').text
Condition = Apple.find('Qualifiers/ItemCondition').text
Price = Apple.find('Price/LandedPrice/Amount').text
print(Date, Condition, Price)


Here's what I receive from my code:



101214 New 19.99
091114 New 21.99


And here is an example of what I would like to see:



Apple 101214 New 19.99
Apple 091114 New 21.99


Ive been fumbling with this for days. I would very much appreciate the help.


No comments:

Post a Comment