Python Lxml (objectify): Checking whether a tag exists
问题 I need to check whether a certain tag exists in an xml file. For example, I want to see if the tag exists in this snippet: <main> <elem1/> <elem2>Hi</elem2> <elem3/> ... </main> Currently, I am using an ugly hack with error checking, like this: try: if root.elem1.tag: foo = elem1 except AttributeError: foo = "error finding elem1" I also want to customize the string if it is unable to find the node (i.e. "unable to find -tagname-"). I have to check a long list of variables, and I don't want to