<objects>
<object>
<record>
<net_amount>3657.82</net_amount>
<order_number>47004603</order_number>
<invoice_source>Email</invoice_source>
<invoice_capture_date>2022-11-13</invoice_capture_date>
<document_type>INVOICE</document_type>
<data_capture_provider_code>00001</data_capture_provider_code>
<data_capture_provider_reference>594826</data_capture_provider_reference>
<document_capture_provide_code>00002</document_capture_provide_code>
<document_capture_provider_ref>594826</document_capture_provider_ref>
</record>
</object>
</objects>
how can i parse this xml data. this data have two "object" elements. when i remove one "object" i am able to parse this. but otherwise i cannot parse it.
for file in files:
tree = ET.parse(file)
root = tree.getroot()
for i in root.findall("record"):
net_amount = i.find("net_amount").text
order_number = i.find("order_number").text
when i use this above code i want to get the "net_amount" and "order_number". but when i remove one object from the xml file it works fine. but i have so many files like this. is there any method to make it work. please help me
CodePudding user response:
You've done the hard part already, all you have to do is to wrap your code in a loop that will go through the object
tags.
for file in files:
tree = ET.parse(file)
root = tree.getroot() #This is the outer "objects" tags
for obj in root.findall("object"): #Loop over all object in it
for i in obj.findall("record"): #Resume the original search in the specific object tag rather than the outer one
net_amount = i.find("net_amount").text
order_number = i.find("order_number").text