LxmlEventWriter¶
- class xsdata.formats.dataclass.serializers.writers.LxmlEventWriter(config, output, ns_map=<factory>)[source]¶
XmlWriter
implementation based on lxml package.Based on the
lxml.sax.ElementTreeContentHandler
, converts sax events to an lxml ElementTree, serialize and write the result to the output stream. Despite that since it’s lxml it’s still pretty fast and has better support for special characters and encodings than native python.- 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
- 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.
- 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.
- end_tag(qname)¶
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)¶
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
- classmethod is_xsi_type(key, value)¶
Return whether the value is an xsi:type or not based on the given attribute name/value.
- 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.