0

I'm using Python 3.7.2 and elementtree to copy the content of a tag in an XML file.

This is my XML file:

<?xml version="1.0" encoding="UTF-8"?>
<Document xmlns="urn:iso:std:iso:20022:tech:xsd:pain.001.003.03">
   <CstmrCdtTrfInitn>
      <GrpHdr>
         <MsgId>nBblsUR-uH..6jmGgZNHLQAAAXgXN1Lu</MsgId>
         <CreDtTm>2016-11-10T12:00:00.000+01:00</CreDtTm> 
         <NbOfTxs>1</NbOfTxs>
         <CtrlSum>6</CtrlSum>
         <InitgPty>
            <Nm>TC 03000 Kunde 55 Protokollr ckf hrung</Nm>
         </InitgPty>
      </GrpHdr>
   </CstmrCdtTrfInitn>
</Document>

I want to copy the content of the 'MsgId' tag and save it as a string. I've manage to do this with minidom before, but due to new circumstances, I have to settle with elementtree for now. This is that code with minidom:

dom = xml.dom.minidom.parse('H:\\app_python/in_spsh/{}'.format(filename_string)) 
message = dom.getElementsByTagName('MsgId')
for MsgId in message:
   print(MsgId.firstChild.nodeValue)

Now I want to do the exact same thing with elementtree. How can I achieve this?

0

1 Answer 1

1

To get the text value of a single element, you can use the findtext() method. The namespace needs to be taken into account.

from xml.etree import ElementTree as ET

tree = ET.parse("test.xml")   # Your XML document
msgid = tree.findtext('.//{urn:iso:std:iso:20022:tech:xsd:pain.001.003.03}MsgId')

With Python 3.8 and later, it is possible to use a wildcard for the namespace:

msgid = tree.findtext('.//{*}MsgId')
Sign up to request clarification or add additional context in comments.

3 Comments

Thank you for your answer. I've tried your and a similar solution but both output 'None' when printed.
This may was silly by me but the original XML is not properly formatted so I deleted some of the tags. I will correct it in the question.
Sorry for wasting your time. It was a stupid typo that caused the issue. Thank you for your help, again!

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.