xsdata.formats.dataclass.serializers.mixins module

class xsdata.formats.dataclass.serializers.mixins.XmlWriterEvent(value)[source]

Bases: enum.Enum

An enumeration.

START = 0
ATTR = 1
DATA = 2
END = 3
class xsdata.formats.dataclass.serializers.mixins.XmlWriter(output, encoding='UTF-8', pretty_print=False, ns_map=<factory>)[source]

Bases: object

Xml writer is an intermediate layer responsible to prepare and buffer the next node information before it’s absolutely ready for the sax content handlers.

Parameters
  • output (TextIO) – The file type object to store the result.

  • handler – Sax content handler

  • encoding (str) –

  • pretty_print (bool) –

  • ns_map (Dict) –

output: TextIO
handler: xml.sax.handler.ContentHandler
encoding: str = 'UTF-8'
pretty_print: bool = False
in_tail: bool = False
tail: Optional[str] = None
attrs: Dict
ns_map: Dict
ns_context: List[Dict]
pending_tag: Optional[Tuple] = None
pending_prefixes: List[List]
write(events)[source]

Iterate over the generated events and feed the sax content handler with the information needed to generate the xml output.

Parameters

events (Generator) –

start_tag(qname)[source]

Start tag notification receiver.

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

Parameters

qname (str) –

add_attribute(key, value)[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
  • key (str) –

  • value (Any) –

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

Parameters

uri (Optional[str]) –

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) –

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) –

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) –

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.