Roberto Jaimes
Roberto Jaimes

Reputation: 105

Check tag existence in python xml.etree.ElementTree module

There is a function of the xml.etree.ElementTree module to do that? NOT:

if node.find(tag)!=None:
    #code
    pass

Upvotes: 0

Views: 533

Answers (1)

alecxe
alecxe

Reputation: 474191

That's exactly what you've presented. find() is the tool for the job:

Finds the first subelement matching match. match may be a tag name or path. Returns an element instance or None.

It's just that, to follow the best practices, use is when comparing to None:

if node.find(tag) is not None:
    # code

Upvotes: 3

Related Questions