XmlWriter#
- class xsdata.formats.dataclass.serializers.mixins.XmlWriter(config, output, ns_map)[source]#
A consistency wrapper for sax content handlers.
Implements a custom sax-like event api with separate start element/attribute events.
Buffers events until all content has been received or a child element is starting in order to build the current element’s namespace context correctly.
Prepares values for serialization.
- Parameters:
config (
SerializerConfig
) – Configuration instanceoutput (
TextIO
) – Output text streamns_map (
Dict
) – User defined namespace prefix-URI map
- write(events)[source]#
Iterate over the generator events and feed the sax content handler with the information needed to generate the xml output.
Example:
(XmlWriterEvent.START, "{http://www.w3.org/1999/xhtml}p"), (XmlWriterEvent.ATTR, "class", "paragraph"), (XmlWriterEvent.DATA, "Hello"), (XmlWriterEvent.END, "{http://www.w3.org/1999/xhtml}p"),
- Parameters:
events (
Generator
) – Events generator
- 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
) – Tag qualified name
- add_attribute(key, value, check_pending=True)[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 is valid and new.
The prefix will be auto generated if it doesn’t exist in the prefix-URI mappings.
- 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
) – 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
) – Tag qualified name
- 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 namespaces prefix mappings and the element with its attributes to the content handler for generation.
- Parameters:
is_nil (
bool
) – If true addxsi:nil="true"
to the element attributes
- start_namespaces()[source]#
Send the new prefixes and namespaces added in the current context 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 exists and the current pending tag is not qualified.