I'm making an API call that returns multiple xml responses as so-
<?xml version="1.0" encoding="UTF-8"?>
<BESAPI xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="BESAPI.xsd">
<Action Resource="https://www.example.com">
<Name> ABC </Name>
<ID> 123 </ID>
</Action>
</BESAPI>
<?xml version="1.0" encoding="UTF-8"?>
<BESAPI xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="BESAPI.xsd">
<Action Resource="https://www.example.com">
<Name> DEF </Name>
<ID> 456 </ID>
</Action>
</BESAPI>
<?xml version="1.0" encoding="UTF-8"?>
<BESAPI xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="BESAPI.xsd">
<Action Resource="https://www.example.com">
<Name> GHI </Name>
<ID> 789 </ID>
</Action>
</BESAPI>
I want to parse all the action IDs from the tag and add them to a list-
import xml.etree.ElementTree as ET
url = ""
payload = ""
headers = {}
response = requests.post(url, headers=headers, data=payload)
root = ET.fromstring(response.content)
actionidlist = []
for elem in root.iter('Action'):
for subelem in elem.iter('ID'):
actionidlist.append(subelem.text)
print(actionidlist)
I get errors though because there are multiple roots. How do I parse this?
Edit: By errors I mean, actionidlist seems to only contain the last ID and not the rest of the IDs.