0

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.

2
  • what is the output of print(elem.findall('Status')) Commented Feb 15, 2021 at 5:44
  • @OsadhiVirochana - I get: [<Element 'Status' at 0x7feafe0ab850>] Commented Feb 15, 2021 at 5:50

2 Answers 2

1

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')) 
Sign up to request clarification or add additional context in comments.

10 Comments

Thank you, that worked!! How do I also print the ActionID from XML Output 2? Right now it only prints the status. I want its corresponding ID. Desired output - 789 and 456
You can get ActionID by adding nope() after actionstatuslist.append(subelem.text). Can you mention what do you want to do so I can write a better code?
For Output 1: Desired output - 123 (This works now) For Output 2: Desired output - 789 456
@jenhenry I changed the code as you mentioned.
Thanks but the edited code only provides computer ID 456. I want 789 also...
|
1
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 .

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.