# XmlWriter

class xsdata.formats.dataclass.serializers.mixins.XmlWriter(config, output, ns_map)

  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 instance

    - output (`TextIO`) – Output text stream

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

  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

  start_document()

    Start document notification receiver.

  start_tag(qname)

    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)

    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

  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

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

  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.

  reset_default_namespace()

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

  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`

  encode_data(data)

    Encode data for xml rendering.

    Return type:

      `Optional`[`str`]