Home > Enterprise >  store content of a tag in a string with elementtree in python3
store content of a tag in a string with elementtree in python3

Time:07-07

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?

CodePudding user response:

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')
  • Related