Skip to content

xml

xsdata.formats.dataclass.serializers.xml

XmlSerializer dataclass

Bases: EventGenerator

Xml serializer for data classes.

Parameters:

Name Type Description Default
config SerializerConfig

The serializer config instance

SerializerConfig()
context XmlContext

The models context instance

XmlContext()
writer Type[XmlWriter]

The xml writer class

default_writer()
Source code in xsdata/formats/dataclass/serializers/xml.py
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
@dataclass
class XmlSerializer(EventGenerator):
    """Xml serializer for data classes.

    Args:
        config: The serializer config instance
        context: The models context instance
        writer: The xml writer class
    """

    writer: Type[XmlWriter] = field(default=default_writer())

    def render(self, obj: Any, ns_map: Optional[Dict] = None) -> str:
        """Serialize the input model instance to xml string.

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

        Returns:
            The serialized xml string output.
        """
        output = StringIO()
        self.write(output, obj, ns_map)
        return output.getvalue()

    def write(self, out: TextIO, obj: Any, ns_map: Optional[Dict] = None):
        """Serialize the given object to the output text stream.

        Args:
            out: The output text stream
            obj: The input model instance to serialize
            ns_map: A user defined namespace prefix-URI map
        """
        events = self.generate(obj)
        handler = self.writer(
            config=self.config,
            output=out,
            ns_map=namespaces.clean_prefixes(ns_map) if ns_map else {},
        )
        handler.write(events)

render(obj, ns_map=None)

Serialize the input model instance to xml string.

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
str

The serialized xml string output.

Source code in xsdata/formats/dataclass/serializers/xml.py
31
32
33
34
35
36
37
38
39
40
41
42
43
def render(self, obj: Any, ns_map: Optional[Dict] = None) -> str:
    """Serialize the input model instance to xml string.

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

    Returns:
        The serialized xml string output.
    """
    output = StringIO()
    self.write(output, obj, ns_map)
    return output.getvalue()

write(out, obj, ns_map=None)

Serialize the given object to the output text stream.

Parameters:

Name Type Description Default
out TextIO

The output text stream

required
obj Any

The input model instance to serialize

required
ns_map Optional[Dict]

A user defined namespace prefix-URI map

None
Source code in xsdata/formats/dataclass/serializers/xml.py
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
def write(self, out: TextIO, obj: Any, ns_map: Optional[Dict] = None):
    """Serialize the given object to the output text stream.

    Args:
        out: The output text stream
        obj: The input model instance to serialize
        ns_map: A user defined namespace prefix-URI map
    """
    events = self.generate(obj)
    handler = self.writer(
        config=self.config,
        output=out,
        ns_map=namespaces.clean_prefixes(ns_map) if ns_map else {},
    )
    handler.write(events)