There are 2 kinds of XML output I may get-
Output 1:
<?xml version="1.0" encoding="UTF-8"?>
<BESAPI xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="BESAPI.xsd">
<Action>
<ActionID>123</ActionID>
</Action>
</BESAPI>
Output 2:
<?xml version="1.0" encoding="UTF-8"?>
<BESAPI xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="BESAPI.xsd">
<Action>
<ActionID>789</ActionID>
<Computer ID="456">
<Status>The action is successful.</Status>
</Computer>
</Action>
</BESAPI>
I want to print Computer Status to a list if present. If not, print Action ID.
This is the code I have but it does not give any result for XML Output 1-
import xml.etree.ElementTree as ET
actionstatuslist = []
doc = ET.parse('actionstatus.xml')
root = doc.getroot()
for elem in root.findall('.//Computer'):
for subelem in elem.findall('Status'):
if subelem is None:
for el in root.iter('Action'):
for subel in el.iter('ActionID'):
actionid = int(subel.text)
print(actionid)
else:
actionstatuslist.append(subelem.text)
Desired result for XML Output 1-
123
I'm getting the correct Computer Status result for XML Output 2 though-
'The action is successful.'
I need help with Output 1 scenario.
print(elem.findall('Status'))[<Element 'Status' at 0x7feafe0ab850>]