I defy you to reliably or easily do this with a regex:
# -*- coding: utf 8 -*-
import xml.etree.ElementTree as et
xml='''\
<data>
<ins rev="REV-NEU" editindex="0">
<insacc rev="c3ce7877-42bf-4c41-b3c0-fd225ccaf512">eins</insacc>
<insacc rev="c3ce7877-42bf-4c41-b3c0-fd225ccaf512">zwei</insacc>
<insacc rev="c3ce7877-42bf-4c41-b3c0-fd225ccaf512">drei</insacc>
<insacc rev="c3ce7877-42bf-4c41-b3c0-fd225ccaf512">vier</insacc>
</ins>
<del rev="REV-NEU" editindex="1">eins</del>
<insacc rev="c3ce7877-42bf-4c41-b3c0-fd225ccaf512">fünf</insacc>
</data>'''
for child in et.fromstring(xml).iter():
print child.tag, child.attrib, child.text
Prints:
data {}
ins {'editindex': '0', 'rev': 'REV-NEU'}
insacc {'rev': 'c3ce7877-42bf-4c41-b3c0-fd225ccaf512'} eins
insacc {'rev': 'c3ce7877-42bf-4c41-b3c0-fd225ccaf512'} zwei
insacc {'rev': 'c3ce7877-42bf-4c41-b3c0-fd225ccaf512'} drei
insacc {'rev': 'c3ce7877-42bf-4c41-b3c0-fd225ccaf512'} vier
del {'editindex': '1', 'rev': 'REV-NEU'} eins
insacc {'rev': 'c3ce7877-42bf-4c41-b3c0-fd225ccaf512'} fünf
If you just want ./ins/insacc
, use xpath:
for child in et.fromstring(xml).findall('./ins/insacc'):
print child.tag, child.attrib, child.text
Prints:
insacc {'rev': 'c3ce7877-42bf-4c41-b3c0-fd225ccaf512'} eins
insacc {'rev': 'c3ce7877-42bf-4c41-b3c0-fd225ccaf512'} zwei
insacc {'rev': 'c3ce7877-42bf-4c41-b3c0-fd225ccaf512'} drei
insacc {'rev': 'c3ce7877-42bf-4c41-b3c0-fd225ccaf512'} vier
If you want all insacc
even at the root:
for child in et.fromstring(xml).iter():
if child.tag=='insacc':
print child.tag, child.attrib, child.text
insacc {'rev': 'c3ce7877-42bf-4c41-b3c0-fd225ccaf512'} eins
insacc {'rev': 'c3ce7877-42bf-4c41-b3c0-fd225ccaf512'} zwei
insacc {'rev': 'c3ce7877-42bf-4c41-b3c0-fd225ccaf512'} drei
insacc {'rev': 'c3ce7877-42bf-4c41-b3c0-fd225ccaf512'} vier
insacc {'rev': 'c3ce7877-42bf-4c41-b3c0-fd225ccaf512'} fünf