XmlEventWriter

class xsdata.formats.dataclass.serializers.writers.XmlEventWriter(config, output, ns_map)[source]

XmlWriter implementation based on native python.

Based on the native python xml.sax.saxutils.XMLGenerator with support for indentation. Converts sax events directly to xml output without storing intermediate result to memory.

Parameters
  • config (SerializerConfig) – Configuration instance

  • output (TextIO) – Output text stream

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

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

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

add_attribute(key, value, check_pending=True)

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
  • key (str) – Attribute name

  • value (Any) – Attribute value

  • check_pending (bool) – Raise exception if not no element is pending start

add_namespace(uri)

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.

Parameters

uri (Optional[str]) – Namespace uri

encode_data(data)

Encode data for xml rendering.

Return type

Optional[str]

flush_start(is_nil=True)

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 add xsi:nil="true" to the element attributes

classmethod is_xsi_type(key, value)

Return whether the value is an xsi:type or not based on the given attribute name/value.

Parameters
  • key (str) – Attribute name

  • value (Any) – Attribute value

Return type

bool

reset_default_namespace()

Reset the default namespace if exists and the current pending tag is not qualified.

set_data(data)

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

start_document()

Start document notification receiver.

start_namespaces()

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.

write(events)

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