Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

xml.etree.ElementTree getElementByID()?

How to get the equivalent of getElementByID() with the Python library xml.etree.ElementTree?

There seems to be a method called parseid() but my tree is already parsed. I don't want to parse it again.

like image 461
guettli Avatar asked Apr 25 '26 23:04

guettli


2 Answers

The accepted answer works indeed, but performance can be quite abysmal as - my guess is, I didn't verify this, perhaps also related to the complexity of xpath - the tree is traversed on every to findall(), which may or may not be a concern for your use case.

Probably parseid() is indeed what you want if performance is a concern. If you want to obtain such an id mapping on an existing tree, you can also easily perform the traversal once manually.

class getElementById():

    def __init__(self, tree):
        self.di = {}

        def v(node):
            i = node.attrib.get("id")
            if i is not None:
                self.di[i] = node

            for child in node:
                v(child)

        v(tree.getroot())


    def __call__(self, k):
        return self.di[k]
like image 183
user1556435 Avatar answered Apr 28 '26 12:04

user1556435


I found it myself:

tree.findall('''.//*[@id='fooID']''')[0]

Better or other solutions are still welcome. :-)

like image 28
guettli Avatar answered Apr 28 '26 11:04

guettli