et_xmlfile.incremental_tree module

class et_xmlfile.incremental_tree.IncrementalTree(element=None, file=None)[source]

Bases: ElementTree

write(file_or_filename, encoding=None, xml_declaration=None, default_namespace=None, method=None, *, short_empty_elements=True, nsmap=None, root_ns_only=False, minimal_ns_only=False)[source]

Write element tree to a file as XML.

Arguments:

file_or_filename – file name or a file object opened for writing

encoding – the output encoding (default: US-ASCII)

xml_declaration – bool indicating if an XML declaration should be

added to the output. If None, an XML declaration is added if encoding IS NOT either of: US-ASCII, UTF-8, or Unicode

default_namespace – sets the default XML namespace (for “xmlns”).

Takes precedence over any default namespace provided in nsmap or xml.etree.ElementTree.register_namespace().

method – either “xml” (default), “html, “text”, or “c14n”

short_empty_elements – controls the formatting of elements

that contain no content. If True (default) they are emitted as a single self-closed tag, otherwise they are emitted as a pair of start/end tags

nsmap – a mapping of namespace prefixes to URIs. These take

precedence over any mappings registered using xml.etree.ElementTree.register_namespace(). The default_namespace argument, if supplied, takes precedence over any default namespace supplied in nsmap. All supplied namespaces will be declared on the root element, even if unused in the document.

root_ns_only – bool indicating namespace declrations should only

be written on the root element. This requires two passes of the xml tree adding additional time to the writing process. This is primarily meant to mimic xml.etree.ElementTree’s behaviour.

minimal_ns_only – bool indicating only namespaces that were used

to qualify elements or attributes should be declared. All namespace declarations will be written on the root element regardless of the value of the root_ns_only arg. Requires two passes of the xml tree adding additional time to the writing process.

et_xmlfile.incremental_tree.compat_tostring(element, encoding=None, method=None, *, xml_declaration=None, default_namespace=None, short_empty_elements=True, nsmap=None, root_ns_only=True, minimal_ns_only=False, tree_cls=<class 'et_xmlfile.incremental_tree.IncrementalTree'>)[source]

tostring with options that produce the same results as xml.etree.ElementTree.tostring

root_ns_only=True is a bit slower than False as it needs to traverse the tree one more time to collect all the namespaces.

et_xmlfile.incremental_tree.current_global_nsmap()[source]
et_xmlfile.incremental_tree.process_attribs(elem, is_nsmap_scope_changed, default_ns_attr_prefix, nsmap_scope, global_nsmap, new_namespace_prefixes, uri_to_prefix)[source]
et_xmlfile.incremental_tree.tostring(element, encoding=None, method=None, *, xml_declaration=None, default_namespace=None, short_empty_elements=True, nsmap=None, root_ns_only=False, minimal_ns_only=False, tree_cls=<class 'et_xmlfile.incremental_tree.IncrementalTree'>)[source]

Generate string representation of XML element.

All subelements are included. If encoding is “unicode”, a string is returned. Otherwise a bytestring is returned.

element is an Element instance, encoding is an optional output encoding defaulting to US-ASCII, method is an optional output which can be one of “xml” (default), “html”, “text” or “c14n”, default_namespace sets the default XML namespace (for “xmlns”).

Returns an (optionally) encoded string containing the XML data.

et_xmlfile.incremental_tree.tostringlist(element, encoding=None, method=None, *, xml_declaration=None, default_namespace=None, short_empty_elements=True, nsmap=None, root_ns_only=False, minimal_ns_only=False, tree_cls=<class 'et_xmlfile.incremental_tree.IncrementalTree'>)[source]
et_xmlfile.incremental_tree.write_elem_start(write, elem, nsmap_scope, global_nsmap, short_empty_elements, is_html, is_root=False, uri_to_prefix=None, default_ns_attr_prefix=None, new_nsmap=None, **kwargs)[source]

Write the opening tag (including self closing) and element text.

Refer to _serialize_ns_xml for description of arguments.

nsmap_scope should be an empty dictionary on first call. All nsmap prefixes must be strings with the default namespace prefix represented by “”.

eg. - <foo attr1=”one”> (returns tag = ‘foo’) - <foo attr1=”one”>text (returns tag = ‘foo’) - <foo attr1=”one” /> (returns tag = None)

Returns:
tag:

The tag name to be closed or None if no closing required.

nsmap_scope:

The current nsmap after any prefix to uri additions from this element. This is the input dict if unmodified or an updated copy.

default_ns_attr_prefix:

The prefix for the default namespace to use with attrs.

uri_to_prefix:

The current uri to prefix map after any uri to prefix additions from this element. This is the input dict if unmodified or an updated copy.

next_remains_root:

A bool indicating if the child element(s) should be treated as their own roots.