xsdata icon indicating copy to clipboard operation
xsdata copied to clipboard

Don't do element.clear() (make it conditional)

Open skinkie opened this issue 1 year ago • 3 comments

element.clear() removes a parsed element from the tree, basically not allowing it to parse it again.

I would like to request te make this behavior conditional.

image

skinkie avatar Feb 26 '24 01:02 skinkie

You can always extend the lxml handler and add your custom logic, the api is pretty concrete and I don't see changing it any time soon. You are parsing from lxml.etree.Element right? why not copy it and keep it somewhere safe before parsing?

I am reluctant to add this because it will impact everyone for an "edge" case, that people can easily overcome with above suggestions.

tefra avatar Feb 26 '24 10:02 tefra

You can always extend the lxml handler and add your custom logic, the api is pretty concrete and I don't see changing it any time soon. You are parsing from lxml.etree.Element right? why not copy it and keep it somewhere safe before parsing?

I think a deep copy is required, which is again an expensive operation. What I am trying to achieve here is to keep the document available which can be used to query information from. With the current xsData behavior that information is gone after parsing the object once.

I am reluctant to add this because it will impact everyone for an "edge" case, that people can easily overcome with above suggestions.

A 'workaround' would be trivial, and I think that should make it into the documentation / examples. To be honest, I don't see why the clear is a good thing in the first place. Could you elaborate on this too?

skinkie avatar Feb 26 '24 10:02 skinkie

It's for saving memory during parsing https://lxml.de/parsing.html#iterparse-and-iterwalk

If you have elements with a long list of children in your XML file and want to save more memory during parsing, you can clean up the preceding siblings of the current element:

tefra avatar Feb 26 '24 18:02 tefra

Closing for now, I will gladly accept a docs contribution

tefra avatar Mar 09 '24 18:03 tefra