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 instanceoutput (
TextIO
) – The output stream to write the resultns_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:
- 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.
- 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.
- add_namespace(uri)[source]#
Add the given uri to the current namespace context.
If the uri empty or a prefix already exists, skip silently.
- 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.