In this Python XML tutorial, you will learn how to use xml.etree.ElementTree package (which is one of the many Python XML parsers) to process XML response.

For this tutorial, you can use your own XML response or follow the steps below and use the one I have provided.

The Python XML parsing steps I’m about to explain and guide you through were developed alongside a refund payment integration I was working on in Django. 

We’re going to explore the parsing end result for the SOAP XML response I wrote about in the previous post. The SOAP XML parsing post was written with a focus on an actual regex parsing in Python and all the steps before you get to the actual XML scheme style response. 

In this post, I’m about to give you a Python XML tutorial for parsing XML schemes when you already have a decoded version of an XML response and you see the information.

The post here is about extracting specific values from the whole XML tree without using regex or any other typical Python parsing tools, but the actual xml.etree.ElementTree package (the right way)

By the way, Roberts Greibers here – in my day job, I’m working as a Python Django backend developer for a local company in Riga, Latvia, and building a white label payment gateway platform. 

Over the past couple of years, I’ve gathered a lot of experience in the FinTech industry, practical knowledge of how payment systems work, how to develop, test and deploy them with minimal headache. 

Of course, I can’t go into too much detail here in a single blog post, but if you want to build your own portfolio project and become a Python and Django developer, I’d suggest checking out client testimonials here and reaching out to me.

I go very deep and always explain all the steps in detail with specific videos and documents for my clients. So the post here is just a small example of how I could help you out.

Also, I always answer all client questions along the way.. but enough of talking, let’s get into the code. 

Response For Python XML Reader

Before we switch to using any Python XML parsers and xml.etree.ElementTree package, let me give you a bit of a context on where all of the following content came from and why would you need to process XML in Python at all. 

How To Get An XML Response

In short, if you’re reading this post I’m assuming you already have an XML response, but read the payment refund flow explanation I wrote here to get an idea of what I had to develop for a recent feature I worked on. 

Okay, after reading the above payment refund flow explanation you should get an idea of what I did with the SOAP XML Response – the end result was an XML response in a string format.

An XML response in string format is not useful at all if you want to extract specific values from the response, for example, payment ID.

So, to solve the XML processing issues – as far as Python3 and its packages goes, you can use xml.etree.ElementTree package or a specifically designed regex pattern in Python.

To get an idea of how’d you do it with regex, learn more about it here or here.

Essentially, using regex in Python for XML parsing is an overkill, so I’d suggest using xml.etree.ElementTree package instead.

XML Scheme Example

As a result of the XML parsing I did in the previously explained post, you’ll get the following XML response string.

But like I mentioned before, having an XML response in a string format is not really that useful, understand the XML response format and let’s move on to the next steps.

xml_response_text = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.002.001.03"><CstmrPmtStsRpt><GrpHdr><MsgId>GW00000032980676</MsgId><CreDtTm>2022-02-11T00:00:00</CreDtTm><InitgPty><Id><OrgId><BICOrBEI>PARXLV22</BICOrBEI></OrgId></Id></InitgPty></GrpHdr><OrgnlGrpInfAndSts><OrgnlMsgId>9G5NSPSQHQ8HGARS</OrgnlMsgId><OrgnlMsgNmId>pain.001.001.03</OrgnlMsgNmId><GrpSts>ACCP</GrpSts></OrgnlGrpInfAndSts><OrgnlPmtInfAndSts><OrgnlPmtInfId>17d273331478416d9fbec2477f55b00e</OrgnlPmtInfId><TxInfAndSts><OrgnlInstrId>ROMCMGCMV5GSAV</OrgnlInstrId><OrgnlEndToEndId>17d273331478416d9fbec2477f55b00e</OrgnlEndToEndId><TxSts>ACCP</TxSts></TxInfAndSts></OrgnlPmtInfAndSts></CstmrPmtStsRpt></Document>'

Here’s the XML response in a pretty print format. Usually, I use a simple XML formatting tool available online or PyCharm autoformatting just to be able to easily read the response content.

<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.002.001.03">
   <CstmrPmtStsRpt>
      <GrpHdr>
         <MsgId>GW00000032980676</MsgId>
         <CreDtTm>2022-02-11T00:00:00</CreDtTm>
         <InitgPty>
            <Id>
               <OrgId>
                  <BICOrBEI>PARXLV22</BICOrBEI>
               </OrgId>
            </Id>
         </InitgPty>
      </GrpHdr>
      <OrgnlGrpInfAndSts>
         <OrgnlMsgId>9G5NSPSQHQ8HGARS</OrgnlMsgId>
         <OrgnlMsgNmId>pain.001.001.03</OrgnlMsgNmId>
         <GrpSts>ACCP</GrpSts>
      </OrgnlGrpInfAndSts>
      <OrgnlPmtInfAndSts>
         <OrgnlPmtInfId>17d273331478416d9fbec2477f55b00e</OrgnlPmtInfId>
         <TxInfAndSts>
            <OrgnlInstrId>ROMCMGCMV5GSAV</OrgnlInstrId>
            <OrgnlEndToEndId>17d273331478416d9fbec2477f55b00e</OrgnlEndToEndId>
            <TxSts>ACCP</TxSts>
         </TxInfAndSts>
      </OrgnlPmtInfAndSts>
   </CstmrPmtStsRpt>
</Document>

Using xml.etree.ElementTree package

The very first step you need to figure out is how you can use Python XML parsers and actually get to a point where you have an object with some kind of attributes ready. 

import xml.etree.ElementTree as ET

xml_response_text = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.002.001.03"><CstmrPmtStsRpt><GrpHdr><MsgId>GW00000032980676</MsgId><CreDtTm>2022-02-11T00:00:00</CreDtTm><InitgPty><Id><OrgId><BICOrBEI>PARXLV22</BICOrBEI></OrgId></Id></InitgPty></GrpHdr><OrgnlGrpInfAndSts><OrgnlMsgId>9G5NSPSQHQ8HGARS</OrgnlMsgId><OrgnlMsgNmId>pain.001.001.03</OrgnlMsgNmId><GrpSts>ACCP</GrpSts></OrgnlGrpInfAndSts><OrgnlPmtInfAndSts><OrgnlPmtInfId>17d273331478416d9fbec2477f55b00e</OrgnlPmtInfId><TxInfAndSts><OrgnlInstrId>ROMCMGCMV5GSAV</OrgnlInstrId><OrgnlEndToEndId>17d273331478416d9fbec2477f55b00e</OrgnlEndToEndId><TxSts>ACCP</TxSts></TxInfAndSts></OrgnlPmtInfAndSts></CstmrPmtStsRpt></Document>'

xml_element: ET.Element = ET.fromstring(xml_response_text)

print(xml_element)

Of course, we’re going to use xml.etree.ElementTree package.

XML .fromstring() Method Tutorial

Take a look at the Python code example above where we’re importing Python’s XML ElementTree package with a shorter name ( ET ) import xml.etree.ElementTree as ET (just to make it shorter and easier to use)

import xml.etree.ElementTree as ET

Obviously, now it’s the time to finally use .fromstring() method from Python xml.etree.ElementTree package.

Python xml.etree.ElementTree.fromstring() method documentation
Python xml.etree.ElementTree.fromstring() method documentation

As you can clearly see from the documentation and also the method name, this is the way to parse a string which is an actual XML, just in a string format to an XML object in Python (exactly what you would want to use in this situation)

And the print in the should give you a representation of an Element object in Python

<Element '{urn:iso:std:iso:20022:tech:xsd:pain.002.001.03}Document' at 0x102a2fc20>

XML .find() Method Tutorial

To be honest, if you’re planning to use xml.etree.ElementTree package for scraping and searching for unknown XML child elements, it’s better to find another package.

Turns out there are no really good ways to access child elements using xml.etree.ElementTree package which I’m surprised about.

Fortunately, in my case, all the XML paths are known and you can pre-define them as strings before you search for them in the XML response.

Which is exactly what I did.

import xml.etree.ElementTree as ET

# Load XML Response text and create xml_element variable
xml_response_text = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.002.001.03"><CstmrPmtStsRpt><GrpHdr><MsgId>GW00000032980676</MsgId><CreDtTm>2022-02-11T00:00:00</CreDtTm><InitgPty><Id><OrgId><BICOrBEI>PARXLV22</BICOrBEI></OrgId></Id></InitgPty></GrpHdr><OrgnlGrpInfAndSts><OrgnlMsgId>9G5NSPSQHQ8HGARS</OrgnlMsgId><OrgnlMsgNmId>pain.001.001.03</OrgnlMsgNmId><GrpSts>ACCP</GrpSts></OrgnlGrpInfAndSts><OrgnlPmtInfAndSts><OrgnlPmtInfId>17d273331478416d9fbec2477f55b00e</OrgnlPmtInfId><TxInfAndSts><OrgnlInstrId>ROMCMGCMV5GSAV</OrgnlInstrId><OrgnlEndToEndId>17d273331478416d9fbec2477f55b00e</OrgnlEndToEndId><TxSts>ACCP</TxSts></TxInfAndSts></OrgnlPmtInfAndSts></CstmrPmtStsRpt></Document>'
xml_element: ET.Element = ET.fromstring(xml_response_text)

# You have to pre-define XML path for 
# the next tag you're looking for. 
# See XML scheme to understand 
# which one is the first XML path in the tree.
cstmr_path: str = (
    '{urn:iso:std:iso:20022:tech:xsd:pain.002.001.03}CstmrPmtStsRpt'
)

# How To Use xml.etree.ElementTree.find() method
cstmr_element: ET.Element = xml_element.find(cstmr_path)
print('cstmr_element: ', cstmr_element)

# Print text of an XML element 
# (if there's no text value, the result is going to be None)
print('cstmr_element.text: ', cstmr_element.text)

The above code example is going to give you an idea of what you need to do to find the “next XML child element” in the XML scheme you have available.

Obviously, there’s no text available for the first element, so the output is not very useful at the moment, it’s: None

xml_element:  <Element '{urn:iso:std:iso:20022:tech:xsd:pain.002.001.03}Document' at 0x10f7fb220>
cstmr_element:  <Element '{urn:iso:std:iso:20022:tech:xsd:pain.002.001.03}CstmrPmtStsRpt' at 0x10f7fb270>
cstmr_element.text:  None

Pre-define XML Path List

Okay, think a few steps ahead, in order to find a text for a specific XML element ( e.g. OrgnlEndToEndId ), you basically need to iterate over the XML tree of elements.

So what I actually did once I realized it is I pre-defined a list of XML paths my Python script has to check in order to find a specific text.

Here’s an example of an OrgnlEndToEndId XML paths list. 

import typing

orgnl_end_to_end_id_path_list: typing.List[str] = [
    '{urn:iso:std:iso:20022:tech:xsd:pain.002.001.03}CstmrPmtStsRpt',
    '{urn:iso:std:iso:20022:tech:xsd:pain.002.001.03}OrgnlPmtInfAndSts',
    '{urn:iso:std:iso:20022:tech:xsd:pain.002.001.03}TxInfAndSts',
    '{urn:iso:std:iso:20022:tech:xsd:pain.002.001.03}OrgnlEndToEndId',
]

If it’s not clear to you how I came up with the list above, check the XML Scheme above, you just have to go through the XML tree element by element. Since you’re able to print the XML path for the first element:

<Element '{urn:iso:std:iso:20022:tech:xsd:pain.002.001.03}Document' at 0x102a2fc20>

It’s very easy to come up with all the other XML paths.

All I did here was I replaced Document with the next element (e.g. CstmrPmtStsRpt ) in the XML tree.

Use For Loop To Find An XML Element Text

Alright, so now that you have an XML path list to find OrgnlEndToEndId element in the whole element tree, it should be already clear the next step is to repeat the previous steps in a classic for loop.

This is a typical Python XML parse method, simply using a for loop you iterate over a list of string XML paths.

I’ve already pretty much explained in detail the code with comments in the code block below, but let me share some details about how this whole code example works down below. 

# From previous code examples above
xml_element: ET.Element = ET.fromstring(xml_response_text)

# Define the initial XML parent element
# (the starting point of iteration)
_parent_element = xml_element

for path in orgnl_end_to_end_id_path_list:

    # Find the next XML element in the XML scheme tree
    # and set it as the next parent element.
    # The idea here is to use element you just found
    # as the next parent element
    _parent_element = _parent_element.find(path)

    # Since you know .text attribute is going to be available
    # only for the actual XML element you're looking for
    # using an if statement with getattr() makes sense here
    if text := getattr(_parent_element, 'text'):
        print('text: ', text)

Having to write for loop iterations is a common Python XML parser practice. Essentially reading XML with Python is going to require you to understand the concept of for loops and also object attributes. 

I believe the very first lines of the code example above are clear from the previous examples, but the interesting part here could be the if statement. Back in the days of Python 2.7 there were no such if statements.

if text := getattr(_parent_element, 'text'):
    print('text: ', text)

This part right here could be confusing if you’ve never seen such if statement before.

So what really is going on here?

To understand line text := getattr(_parent_element, 'text') you first have to understand the condition of built-in getattr() function. 

Python getattr documentation
Python getattr documentation

In short getattr() function searches for an attribute of an object. If the attribute is not found it returns None. If it is found, getattr() function returns the value of that attribute.

In our case, it fits perfectly the goal of reading XML with Python, you want to find an XML element with .text attribute that is not empty. 

text := getattr(_parent_element, 'text') 

Value will be only assigned to text variable here, if _parent_element will have a text attribute. That’s the power of text :=

Python bool() function documentation
Python bool() function documentation

And code under if statement is going to be executed only if the value in the text is going to evaluate truthfully – meaning it equals to True when built-in bool() function would be applied to it. 

Python XML Reader Function

In the end, to build a proper Python XML parser, you’ll need to refactor the above solution. Here’s what I came up with, a simple xml_read_element_text() function.

Takes in an element argument with path_list. The element argument here is supposed to be the initial Python XML (Read from a string XML to Python XML)

import typing
import xml.etree.ElementTree as ET

# Define a function to find XML key value
# Refactored version of previously given for loop
def xml_read_element_text(
    element: ET.Element,
    path_list: typing.List[str]
) -> typing.Optional[str]:
    _parent_element = element

    for path in path_list:

        _parent_element = _parent_element.find(path)

        if text := getattr(_parent_element, 'text'):
            return text


# Load XML in Python,
# make it available through xml_element variable
xml_response_text: str = '<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.002.001.03"><CstmrPmtStsRpt><GrpHdr><MsgId>GW00000032980676</MsgId><CreDtTm>2022-02-11T00:00:00</CreDtTm><InitgPty><Id><OrgId><BICOrBEI>PARXLV22</BICOrBEI></OrgId></Id></InitgPty></GrpHdr><OrgnlGrpInfAndSts><OrgnlMsgId>9G5NSPSQHQ8HGARS</OrgnlMsgId><OrgnlMsgNmId>pain.001.001.03</OrgnlMsgNmId><GrpSts>ACCP</GrpSts></OrgnlGrpInfAndSts><OrgnlPmtInfAndSts><OrgnlPmtInfId>17d273331478416d9fbec2477f55b00e</OrgnlPmtInfId><TxInfAndSts><OrgnlInstrId>ROMCMGCMV5GSAV</OrgnlInstrId><OrgnlEndToEndId>17d273331478416d9fbec2477f55b00e</OrgnlEndToEndId><TxSts>ACCP</TxSts></TxInfAndSts></OrgnlPmtInfAndSts></CstmrPmtStsRpt></Document>'
xml_element: ET.Element = ET.fromstring(xml_response_text)

# Define XML path list to find a specific key value
# In this case, we want to find OrgnlEndToEndId value
orgnl_end_to_end_id_path_list: typing.List[str] = [
    '{urn:iso:std:iso:20022:tech:xsd:pain.002.001.03}CstmrPmtStsRpt',
    '{urn:iso:std:iso:20022:tech:xsd:pain.002.001.03}OrgnlPmtInfAndSts',
    '{urn:iso:std:iso:20022:tech:xsd:pain.002.001.03}TxInfAndSts',
    '{urn:iso:std:iso:20022:tech:xsd:pain.002.001.03}OrgnlEndToEndId',
]

# Use the xml_read_element_text function
# to find OrgnlEndToEndId value and assign it to a variable
orgnl_end_to_end_id: str = (
    xml_read_element_text(
        element=xml_element,
        path_list=orgnl_end_to_end_id_path_list
    )
)

print('orgnl_end_to_end_id: ', orgnl_end_to_end_id)

Pay attention, the above code example of reading XML with Python is a summary and refactored version of all the previous steps I explained above.

This should be totally enough to get you started. And here’s the output you should see after the execution of the above code example.

orgnl_end_to_end_id:  17d273331478416d9fbec2477f55b00e

If you want to see the final version, keep reading the next section. 

Python XML Parser (Building The Final Version)

Finally, once you figure out the way of reading XML with Python for one of the key values, you usually have to do the same steps for another key value. 

Once you know it’s the case, it’s always better to come up with a Python class in order to keep everything organized.

In the tutorial here, I’ll create a Python XML parser class and show you how I would refactor everything mentioned before into a clean Python XML reader class. 

Create Python XML Parser Class

Beginning here is straightforward, create a new XMLParser class and define a new __init__ method. 

What’s the purpose of  __init__ method?

Python class __init__() method
Python class __init__() method

This is the code that’s going to be executed at the moment of class object creation. And obviously, at the moment of XMLParser definition, you want to read XML with Python. 

That’s why the following line: self.element: ET.Element = ET.fromstring(_xml) is being used. 

import typing
import xml.etree.ElementTree as ET

class XMLParser:

    document = '{urn:iso:std:iso:20022:tech:xsd:pain.002.001.03}'

    def __init__(self, _xml: str):
        self.xml = _xml

        try:
            self.element: ET.Element = ET.fromstring(_xml)
        except ET.ParseError:
            self.element = None

Also, the document = '{urn:iso:std:iso:20022:tech:xsd:pain.002.001.03}' variable here is to simplify XML path definition.

If you didn’t notice that yet, it’s the same string value used over and over again. You don’t want to just copy-paste the same string everywhere, it’s easier to have a variable definition and then just use the variable. 

Reading XML With Python Class (Inside __init__ Method)

You might ask, why try/except block.

try:
    self.element: ET.Element = ET.fromstring(_xml)
except ET.ParseError:
    self.element = None

Well, depending on your use case you might run into a situation when reading XML that raises an exception. That would happen in the case you pass an invalid XML string to the Python XML reader. 

Handling Exceptions In Python
Handling Exceptions In Python

try/except statement lets you handle such a situation and in my case, it was fine to set self.element = None. If I’d ever see self.element = None I would know something went wrong with XML parsing in Python. 

Python XML Parse Method Defenition

The previously defined method xml_read_element_text() has now been refactored to parse(). And the difference here is you have now access to self and all the variables within the XML Parser class. 

import typing
import xml.etree.ElementTree as ET

class XMLParser:

    document = '{urn:iso:std:iso:20022:tech:xsd:pain.002.001.03}'

    def __init__(self, _xml: str):
        self.xml = _xml

        try:
            self.element: ET.Element = ET.fromstring(_xml)
        except ET.ParseError:
            self.element = None

    def parse(
        self,
        path_list: typing.List[str]
    ) -> typing.Optional[str]:
        _parent_element = self.element

        for path in path_list:

            _parent_element = _parent_element.find(path)

            if text := getattr(_parent_element, 'text'):
                return text

Having access to self is a major refactoring advantage. You can define commonly used variables in __init__ method and re-use them throughout the whole XML Parser class. 

In short, that’s the main advantage of moving and organizing your code into classes like XMLParser class.

Define Method To Reading XML (Specific Key Value)

And finally, you want to define read_orgnl_end_to_end_id() method which is using self.parse() method.

You can already see how easy it’s going to be to add just another method with a slightly different path list and find a way of reading XML in Python.

import typing
import xml.etree.ElementTree as ET

class XMLParser:

    document = '{urn:iso:std:iso:20022:tech:xsd:pain.002.001.03}'

    def __init__(self, _xml: str):
        self.xml = _xml

        try:
            self.element: ET.Element = ET.fromstring(_xml)
        except ET.ParseError:
            self.element = None

    def parse(
        self,
        path_list: typing.List[str]
    ) -> typing.Optional[str]:
        _parent_element = self.element

        for path in path_list:

            _parent_element = _parent_element.find(path)

            if text := getattr(_parent_element, 'text'):
                return text

    def read_orgnl_end_to_end_id(self) -> str:
        return self.parse(
            path_list=[
                f'{self.document}CstmrPmtStsRpt',
                f'{self.document}OrgnlPmtInfAndSts',
                f'{self.document}TxInfAndSts',
                f'{self.document}OrgnlEndToEndId',
            ]
        )

xml_parser = XMLParser(
    _xml='<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.002.001.03"><CstmrPmtStsRpt><GrpHdr><MsgId>GW00000032980676</MsgId><CreDtTm>2022-02-11T00:00:00</CreDtTm><InitgPty><Id><OrgId><BICOrBEI>PARXLV22</BICOrBEI></OrgId></Id></InitgPty></GrpHdr><OrgnlGrpInfAndSts><OrgnlMsgId>9G5NSPSQHQ8HGARS</OrgnlMsgId><OrgnlMsgNmId>pain.001.001.03</OrgnlMsgNmId><GrpSts>ACCP</GrpSts></OrgnlGrpInfAndSts><OrgnlPmtInfAndSts><OrgnlPmtInfId>17d273331478416d9fbec2477f55b00e</OrgnlPmtInfId><TxInfAndSts><OrgnlInstrId>ROMCMGCMV5GSAV</OrgnlInstrId><OrgnlEndToEndId>17d273331478416d9fbec2477f55b00e</OrgnlEndToEndId><TxSts>ACCP</TxSts></TxInfAndSts></OrgnlPmtInfAndSts></CstmrPmtStsRpt></Document>'
)

orgnl_end_to_end_id: str = xml_parser.read_orgnl_end_to_end_id()

print('orgnl_end_to_end_id: ', orgnl_end_to_end_id)

In the end, you should have the same output, it’s just that the code is now refactored and a bit more organized.

orgnl_end_to_end_id:  17d273331478416d9fbec2477f55b00e

And the fact that the XML parsing code is now organized, gives you more flexibility for reading XML in Python. It’s very easy to add new XMLParser class methods to read XML and output other key values.

Reading XML Of Other Key Values

The final, final solution here is to add second and third methods to reading XML in Python. After this example, it should be really easy for you to add another method if you’d want to read another value. 

import typing
import xml.etree.ElementTree as ET

class XMLParser:

    document = '{urn:iso:std:iso:20022:tech:xsd:pain.002.001.03}'

    def __init__(self, _xml: str):
        self.xml = _xml

        try:
            self.element: ET.Element = ET.fromstring(_xml)
        except ET.ParseError:
            self.element = None

    def parse(
        self,
        path_list: typing.List[str]
    ) -> typing.Optional[str]:
        _parent_element = self.element

        for path in path_list:

            _parent_element = _parent_element.find(path)

            if text := getattr(_parent_element, 'text'):
                return text

    def read_orgnl_end_to_end_id(self) -> str:
        return self.parse(
            path_list=[
                f'{self.document}CstmrPmtStsRpt',
                f'{self.document}OrgnlPmtInfAndSts',
                f'{self.document}TxInfAndSts',
                f'{self.document}OrgnlEndToEndId',
            ]
        )

    def read_status(self) -> str:
        return self.parse(
            path_list=[
                f'{self.document}CstmrPmtStsRpt',
                f'{self.document}OrgnlPmtInfAndSts',
                f'{self.document}TxInfAndSts',
                f'{self.document}TxSts',
            ]
        )

    def read_orgnl_msg_id(self) -> str:
        return self.parse(
            path_list=[
                f'{self.document}CstmrPmtStsRpt',
                f'{self.document}OrgnlGrpInfAndSts',
                f'{self.document}OrgnlMsgId',
            ]
        )

xml_parser = XMLParser(
    _xml='<?xml version="1.0" encoding="UTF-8" standalone="yes"?><Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.002.001.03"><CstmrPmtStsRpt><GrpHdr><MsgId>GW00000032980676</MsgId><CreDtTm>2022-02-11T00:00:00</CreDtTm><InitgPty><Id><OrgId><BICOrBEI>PARXLV22</BICOrBEI></OrgId></Id></InitgPty></GrpHdr><OrgnlGrpInfAndSts><OrgnlMsgId>9G5NSPSQHQ8HGARS</OrgnlMsgId><OrgnlMsgNmId>pain.001.001.03</OrgnlMsgNmId><GrpSts>ACCP</GrpSts></OrgnlGrpInfAndSts><OrgnlPmtInfAndSts><OrgnlPmtInfId>17d273331478416d9fbec2477f55b00e</OrgnlPmtInfId><TxInfAndSts><OrgnlInstrId>ROMCMGCMV5GSAV</OrgnlInstrId><OrgnlEndToEndId>17d273331478416d9fbec2477f55b00e</OrgnlEndToEndId><TxSts>ACCP</TxSts></TxInfAndSts></OrgnlPmtInfAndSts></CstmrPmtStsRpt></Document>'
)

status: str = xml_parser.read_status()
orgnl_msg_id: str = xml_parser.read_orgnl_msg_id()
orgnl_end_to_end_id: str = xml_parser.read_orgnl_end_to_end_id()

print('status: ', status)
print('orgnl_msg_id: ', orgnl_msg_id)
print('orgnl_end_to_end_id: ', orgnl_end_to_end_id)

This is the end result I came up with and it worked perfectly for my situation. Here’s the last output from execution of above code example. 

status:  ACCP
orgnl_msg_id:  9G5NSPSQHQ8HGARS
orgnl_end_to_end_id:  17d273331478416d9fbec2477f55b00e

Conclusion

Alright, so that’s pretty much it! 

There you have it, all the steps you need to take in order to parse XML response and have easy access to objects in the response. 

Can You Become A Python Developer?

So when you’re reading a Python tutorial like in this post and feel like you also would like to become a Python Django developer… 🐍

…then let’s schedule a quick call with me personally, you can find the information somewhere down below to do that.

Find a way to contact me, tell me about your situation… 📲

…and I’ll see if I can do something about it. 

If You Have Trouble Learning On Your Own

Or if you’re already trying to follow the steps in this post and have errors you don’t know how to solve – join the Discord server and tell me about your situation there. 

If we do jump on a call, I’ll be looking at your situation I’ll be analyzing your current coding skill level, I can tell you what exact learning steps would fit for you and your specific situation, what milestones you need to reach in order to get started, and get your first Python job opportunities. 

If You Want To Learn More About Python

Even if you already feel you know Python or Django and you want to learn more – this call is also for you!

If you feel a bit lost, you have no idea what you could be learning to become a Python Django developer, this call is for you as well!  🔥

I can help you out with making sure you’re going in the right direction!

And even if you already know what it is that you should be learning, but you’re struggling on your own, let’s discuss your situation, just send me a quick message. 

Talk to you soon!

I'll help you become a Python developer!

If you're interested in learning Python and getting a job as a Python developer, send me an email to roberts.greibers@gmail.com and I'll see if I can help you.

Roberts Greibers

Roberts Greibers

I help QA engineers to become backend Python/Django developers so they can increase their income