How can I check the existence of attributes and tags in XML before parsing?
Asked Answered
B

2

45

I'm parsing an XML file via Element Tree in python and and writing the content to a cpp file.

The content of children tags will be variant for different tags. For example first event tag has party tag as child but second event tag doesn't have.

  • How can I check whether a tag exists or not before parsing?

  • Children has value attribute in 1st event tag but not in second. How can I check whether an attribute exists or not before taking it's value.

Currently my code throws an error for non existing party tag and sets a "None" attribute value for the second children tag.

<main>
  <event>
    <party>Big</party>
    <children type="me" value="3"/>
  </event>

  <event>
    <children type="me"/>
  </event>

</main>

Code:

import xml.etree.ElementTree as ET
tree = ET.parse('party.xml')
root = tree.getroot()
for event in root.findall('event'):
    parties = event.find('party').text
    children = event.get('value')

I want to check the tags and then take their values.

Blare answered 22/3, 2013 at 10:30 Comment(0)
I
82

If a tag doesn't exist, .find() indeed returns None. Simply test for that value:

for event in root.findall('event'):
    party = event.find('party')
    if party is None:
        continue
    parties = party.text
    children = event.get('value')

You already use .get() on event to test for the value the attribute; it returns None as well if the attribute does not exist.

Attributes are stored in the .attrib dictionary, so you can use standard Python techniques to test for the attribute explicitly too:

if 'value' in event.attrib:
    # value attribute is present.
Isaacs answered 22/3, 2013 at 10:33 Comment(3)
@Martjin Pieters You mentioned if party is None: How will I write in Python if i wish to write something like -- If party!= None:Blare
@Abhishek: if party is not None. See python comparison operators.Isaacs
Thanks for also mentioning how to check for attributes. Very good answer.Glade
O
1

Since Python 3.8, we can use the walrus operator := here to "simplify" the checking process. With an if statement, we check if the tag or attribute exists and proceed only if it does.

tree = ET.parse('party.xml')
root = tree.getroot()
for event in root.findall('event'):
    if (party := event.find('party')) is not None:   # <--- check if `party` tag exists
        parties = party.text
        print(parties)
    if (children := event.get('value')) is not None: # <--- check if `value` attribute exists
        print(children)
Ozenfant answered 15/1 at 4:48 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.