Is there an ElementTree class lookup hook?
![](https://secure.gravatar.com/avatar/bad73a18a6cce33fce0ebd868981313b.jpg?s=120&d=mm&r=g)
lxml provides support for custom Element classes (as well as element-ish e.g. Comment or PI) via the `ElementDefaultClassLookup` registry, and the ability to hook it into a parser. But that registry does not seem to have a slot for the root tree of the elements. Is there a hook somewhere to set *that*? I tried looking around the API docs but nothing really jumped out. PS: the documentation for `set_default_parser` explains that it sets the default parser *for the current thread* and that "You can create a separate parser for each thread explicitly or use a parser pool.", does it mean that in a "don't call any API which gets an implicit parser and manage your parsers by hand" sense or something else?
![](https://secure.gravatar.com/avatar/8b97b5aad24c30e4a1357b38cc39aeaa.jpg?s=120&d=mm&r=g)
Salut encore, Xavier Morel schrieb am 04.03.22 um 12:58:
lxml provides support for custom Element classes (as well as element-ish e.g. Comment or PI) via the `ElementDefaultClassLookup` registry, and the ability to hook it into a parser.
But that registry does not seem to have a slot for the root tree of the elements. Is there a hook somewhere to set *that*? I tried looking around the API docs but nothing really jumped out.
Do you really need something like that? Can't you just inherit from the ElementTree class? (Assuming that's what you meant.) The reason why you can register your own Element classes is because they can appear all over the place in the API. The ElementTree class is either instantiated by the user or returned from the parse() function. That's mostly it. Ok, maybe XSLT. But still easy enough to wrap yourself.
PS: the documentation for `set_default_parser` explains that it sets the default parser *for the current thread* and that "You can create a separate parser for each thread explicitly or use a parser pool.", does it mean that in a "don't call any API which gets an implicit parser and manage your parsers by hand" sense or something else?
Parsers are really only used where an explicit "parser" argument is accepted. Everything else just inherits them. If you want to use your own parser, write a wrapper function for parse() that always passes it in, and then use that function instead. Stefan
participants (2)
-
Stefan Behnel
-
Xavier Morel