File: tree.py

package info (click to toggle)
python-xsdata 26.2-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 3,200 kB
  • sloc: python: 31,234; xml: 422; makefile: 20; sh: 6
file content (34 lines) | stat: -rw-r--r-- 1,032 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
from dataclasses import dataclass
from typing import Any

from lxml.etree import ElementTree

from xsdata.formats.dataclass.serializers.mixins import EventGenerator
from xsdata.formats.dataclass.serializers.writers.lxml import LxmlTreeBuilder
from xsdata.utils import namespaces


@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: dict | None = 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))