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_XML_WRITER`

Source code in `xsdata/formats/dataclass/serializers/xml.py`

    13
    14
    15
    16
    17
    18
    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

    @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_XML_WRITER)

        def render(self, obj: Any, ns_map: dict | None = 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: TextIOBase, obj: Any, ns_map: dict | None = None) -> 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` | `dict | None` | 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`

    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37

    def render(self, obj: Any, ns_map: dict | None = 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` | `TextIOBase` | The output text stream | required
`obj` | `Any` | The input model instance to serialize | required
`ns_map` | `dict | None` | A user defined namespace prefix-URI map | `None`

Source code in `xsdata/formats/dataclass/serializers/xml.py`

    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53

    def write(self, out: TextIOBase, obj: Any, ns_map: dict | None = None) -> 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)