jenhenry
jenhenry

Reputation: 23

Python continue if xml element name is not present

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.

Upvotes: 0

Views: 73

Answers (2)

Ajay
Ajay

Reputation: 5347

import xml.etree.ElementTree as ET
actionstatuslist = []
doc = ET.parse('actionstatus.xml')
root = doc.getroot()

for child in root.iter('Action'):
    grand_child=child.find("Computer/Status")
    if grand_child != None:
        actionstatuslist.append(grand_child.text)
    else:
        temp=child.find("ActionID")
        actionstatuslist.append(temp.text)

First we've got the Action tag inside child and we are looking for Computer/Status,using a if or we are going to just get the ActionID .

Upvotes: 1

Osadhi Virochana
Osadhi Virochana

Reputation: 1302

Because of root.findall('.//Computer') returns empty list([]) it doesn't execute

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)

So at first, you need to check if root.findall('.//Computer') is empty and then check if elem.findall('Status') is empty. If one if them are empty then run

for el in root.iter('Action'):
    for subel in el.iter('ActionID'):
        actionid = int(subel.text)
        print(actionid)

It is easier if you use it as a function

Final code will be like this:

import xml.etree.ElementTree as ET
actionstatuslist = []
doc = ET.parse('actionstatus.xml')
root = doc.getroot()

def get_action_id():
    for el in root.iter('Action'):
        for subel in el.iter('ActionID'):
            actionid = int(subel.text)
            print(actionid)
    

if root.findall('.//Computer'):
    for elem in root.findall('.//Computer'):
        if elem.findall('Status'):
            for subelem in elem.findall('Status'):
                actionstatuslist.append(subelem.text)
        else:
            get_action_id()               
else:    
    get_action_id()

Edit in final code(Added requested features in comments):

import xml.etree.ElementTree as ET
actionstatuslist = []
doc = ET.parse('actionstatus.xml')
root = doc.getroot()

def get_action_id():
    for el in root.iter('Action'):
        for subel in el.iter('ActionID'):
            actionid = int(subel.text)
            print(actionid)
    
get_action_id()
for elem in root.findall('.//Computer'):
    if elem.findall('Status'):
        for subelem in elem.findall('Status'):
            actionstatuslist.append(subelem.text)
            print(elem.get('ID')) 

Upvotes: 1

Related Questions