XmlWriter#

class xsdata.formats.dataclass.serializers.mixins.XmlWriter(config, output, ns_map)[source]#

A consistency wrapper for sax content handlers.

Parameters:
  • config (SerializerConfig) – The serializer config instance

  • output (TextIO) – The output stream to write the result

  • ns_map (Dict) – A user defined namespace prefix-URI map

handler#

The content handler instance

in_tail#

Specifies whether the text content has been written

tail#

The current element tail content

attrs#

The current element attributes

ns_context#

The namespace context queue

pending_tag#

The pending element namespace, name tuple

pending_prefixes#

The pending element namespace prefixes

abstract build_handler()[source]#

Build the content handler instance.

Return type:

ContentHandler

Returns:

A content handler instance.

write(events)[source]#

Feed the sax content handler with events.

The receiver will also add additional root attributes like xsi or no namespace location.

Parameters:

events (Iterator[Union[Tuple[Literal['start'], str], Tuple[Literal['attr'], str, Any], Tuple[Literal['data'], str], Tuple[Literal['end'], str]]]) – An iterator of sax events

Raises:

XmlWriterError – On unknown events.

start_document()[source]#

Start document notification receiver.

Write the xml version and encoding, if the configuration is enabled.

start_tag(qname)[source]#

Start tag notification receiver.

The receiver will flush the start of any pending element, create new namespaces context and queue the current tag for generation.

Parameters:

qname (str) – The qualified name of the starting element

add_attribute(qname, value, root=False)[source]#

Add attribute notification receiver.

The receiver will convert the key to a namespace, name tuple and convert the value to string. Internally the converter will also generate any missing namespace prefixes.

Parameters:
  • qname (str) – The qualified name of the attribute

  • value (Any) – The value of the attribute

  • root (bool) – Specifies if attribute is for the root element

Raises:

XmlWriterError – If it’s not a root element attribute and not no element is pending to start.

add_namespace(uri)[source]#

Add the given uri to the current namespace context.

If the uri empty or a prefix already exists, skip silently.

Parameters:

uri (Optional[str]) – The namespace URI

set_data(data)[source]#

Set data notification receiver.

The receiver will convert the data to string, flush any previous pending start element and send it to the handler for generation.

If the text content of the tag has already been generated then treat the current data as element tail content and queue it to be generated when the tag ends.

Parameters:

data (Any) – The element text or tail content

end_tag(qname)[source]#

End tag notification receiver.

The receiver will flush if pending the start of the element, end the element, its tail content and its namespaces prefix mapping and current context.

Parameters:

qname (str) – The qualified name of the element

flush_start(is_nil=True)[source]#

Flush start notification receiver.

The receiver will pop the xsi:nil attribute if the element is not empty, prepare and send the namespace prefix-URI map and the element with its attributes to the content handler for generation.

Parameters:

is_nil (bool) – Specify if the element requires xsi:nil=”true” when content is empty

start_namespaces()[source]#

Send the current namespace prefix-URI map to the content handler.

Save the list of prefixes to be removed at the end of the current pending tag.

reset_default_namespace()[source]#

Reset the default namespace if the pending element is not qualified.

classmethod is_xsi_type(qname, value)[source]#

Return whether the value is a xsi:type.

Parameters:
  • qname (str) – The attribute qualified name

  • value (Any) – The attribute value

Return type:

bool

Returns:

The bool result.

encode_data(data)[source]#

Encode data for xml rendering.

Parameters:

data (Any) – The content to encode/serialize

Return type:

Optional[str]

Returns:

The xml encoded data