Home > Software design >  Select XML tags that have certain tags inside them
Select XML tags that have certain tags inside them

Time:09-29

I am parsing an xml file with Python using ElementTree, and I need to select tags like <PostalAddressText> below that have tags like <insert> inside them. How do I do that? I need to get a list of all relevant tag names in the xml file that satisfy the condition.

Here's a fragment of XML that I am parsing (the actual text except for inside tags has been replaced with Lorem Ipsum):

<?xml version="1.0"?>
<data>
    <PostalAddressText>123456,
    <insert>
</insert>Lorem ipsum dolor sit amet, <insert>
</insert>consectetur adipiscing elit.<insert>
</insert>Etiam cursus ligula non malesuada fringilla.<delete> </delete><insert>
</insert>Quisque porta quam eu finibus pulvinar.<delete>or</delete><insert>er</insert> Mauris at semper urna.<delete>a</delete><insert>o</insert> Donec feugiat<delete>arcu purus</delete><insert>et lacinia</insert></PostalAddressText>
    <PersonNameText>789012,
    <insert>
</insert>Lorem ipsum dolor sit amet, <insert>
</insert>consectetur adipiscing elit.<insert>
</insert>Etiam cursus ligula non malesuada fringilla.<delete> </delete><insert>
</insert>Quisque porta quam eu finibus pulvinar.<delete>or</delete><insert>er</insert> Mauris at semper urna.<delete>a</delete><insert>o</insert> Donec feugiat<delete>arcu purus</delete><insert>et lacinia</insert>
    </PersonNameText>
</data>

I have tried this but nothing gets printed to the console:

test_data = root.findall(".//")
for el in test_data:
    if el.text == '*<insert>*':
        print(el.tag, el.text)

CodePudding user response:

Not sure what is your expected output but see the code and output below

import xml.etree.ElementTree as ET

xml = '''<?xml version="1.0"?>
<data>
    <PostalAddressText>123456,
    <insert>
</insert>Lorem ipsum dolor sit amet, <insert>
</insert>consectetur adipiscing elit.<insert>
</insert>Etiam cursus ligula non malesuada fringilla.<delete> </delete><insert>
</insert>Quisque porta quam eu finibus pulvinar.<delete>or</delete><insert>er</insert> Mauris at semper urna.<delete>a</delete><insert>o</insert> Donec feugiat<delete>arcu purus</delete><insert>et lacinia</insert></PostalAddressText>
    <PersonNameText>789012,
    <insert>
</insert>Lorem ipsum dolor sit amet, <insert>
</insert>consectetur adipiscing elit.<insert>
</insert>Etiam cursus ligula non malesuada fringilla.<delete> </delete><insert>
</insert>Quisque porta quam eu finibus pulvinar.<delete>or</delete><insert>er</insert> Mauris at semper urna.<delete>a</delete><insert>o</insert> Donec feugiat<delete>arcu purus</delete><insert>et lacinia</insert>
    </PersonNameText>
</data>'''

root = ET.fromstring(xml)
for idx, insert in enumerate(root.findall('.//insert'), 1):
    print(f'{idx}) {insert.text}  {insert.tail}')

output

1) 
  Lorem ipsum dolor sit amet, 
2) 
  consectetur adipiscing elit.
3) 
  Etiam cursus ligula non malesuada fringilla.
4) 
  Quisque porta quam eu finibus pulvinar.
5) er   Mauris at semper urna.
6) o   Donec feugiat
7) et lacinia  None
8) 
  Lorem ipsum dolor sit amet, 
9) 
  consectetur adipiscing elit.
10) 
  Etiam cursus ligula non malesuada fringilla.
11) 
  Quisque porta quam eu finibus pulvinar.
12) er   Mauris at semper urna.
13) o   Donec feugiat
14) et lacinia  

CodePudding user response:

I got the result that I needed with the following code (I got the idea from @balderman):

for el in root.findall('.//*[insert]'): 
    print(el.tag)

Which gave me the name of the tag.

  • Related