Skip to content

tree

xsdata.formats.dataclass.serializers.tree

TreeSerializer dataclass

Bases: EventGenerator

Lxml tree serializer for data classes.

Parameters:

Name Type Description Default
config SerializerConfig

The serializer config instance

SerializerConfig()
context XmlContext

The models context instance

XmlContext()
Source code in xsdata/formats/dataclass/serializers/tree.py
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
@dataclass
class TreeSerializer(EventGenerator):
    """Lxml tree serializer for data classes.

    Args:
        config: The serializer config instance
        context: The models context instance
    """

    def render(self, obj: Any, ns_map: Optional[Dict] = None) -> ElementTree:
        """Serialize the input model instance to a lxml etree instance.

        Args:
            obj: The input model instance to serialize
            ns_map: A user defined namespace prefix-URI map

        Returns:
            The element tree instance.
        """
        builder = LxmlTreeBuilder(
            config=self.config,
            ns_map=namespaces.clean_prefixes(ns_map) if ns_map else {},
        )
        return builder.build(self.generate(obj))

render(obj, ns_map=None)

Serialize the input model instance to a lxml etree instance.

Parameters:

Name Type Description Default
obj Any

The input model instance to serialize

required
ns_map Optional[Dict]

A user defined namespace prefix-URI map

None

Returns:

Type Description
ElementTree

The element tree instance.

Source code in xsdata/formats/dataclass/serializers/tree.py
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
def render(self, obj: Any, ns_map: Optional[Dict] = None) -> ElementTree:
    """Serialize the input model instance to a lxml etree instance.

    Args:
        obj: The input model instance to serialize
        ns_map: A user defined namespace prefix-URI map

    Returns:
        The element tree instance.
    """
    builder = LxmlTreeBuilder(
        config=self.config,
        ns_map=namespaces.clean_prefixes(ns_map) if ns_map else {},
    )
    return builder.build(self.generate(obj))