Hi folks,

lxml.objectfy lets us define custom types by means of objectify.PyType and ObjectifiedDataElement sub classes, e.g. as in [0]. It's nice how they map to XSD and automatically convert between python type and XML representation.

I understand how it works for scalar leaf nodes. But does objectify.PyType work for structured/nested types as well? Or is PyType the wrong tool and one should better head over to "Generating XML with custom classes" [1] and "Custom element class lookup" [2]? Or is it not possible at all?

Simple imaginary code:

---snip---
from collections import namedtuple
from lxml import etree, objectify

# A structured python type
MyStructuredThing = namedtuple('MyStructuredThing', 'a b')

# some custom code and registration like with objectify.PyType here
# ...

root = objectify.Element("root")
# magically take python type and construct tree + leaf elements automagically
root.mystructuredthing = MyStructuredThing(1, 2)
etree.tostring(root, pretty_print=True)

---snap---

Expected output:

<root>
    <mystructuredthing>
        <a>1</a>
        <b>1</b>
    </mystructuredthing>
<root>

etree.fromstring should in turn deserialize this XML snippet, so that isinstance(root.mystructuredthing.pyval, MyStructuredThing) == True.

Thanks
Tobias

[0] https://lxml.de/api/lxml.tests.test_objectify-pysrc.html#ObjectifyTestCase.test_registered_type_stringify [1] https://lxml.de/element_classes.html#generating-xml-with-custom-classes
[2] https://lxml.de/element_classes.html#custom-element-class-lookup
_______________________________________________
lxml - The Python XML Toolkit mailing list -- lxml@python.org
To unsubscribe send an email to lxml-le...@python.org
https://mail.python.org/mailman3/lists/lxml.python.org/
Member address: arch...@mail-archive.com

Reply via email to