Python Glossary Русский

Python 3.8+xml.etree.ElementTreeterm

xml.etree.ElementTree.C14NWriterTarget

canonicalization

An XMLParser target that builds no tree and writes canonical C14N 2.0 output straight through the given write function.

Syntax

target = ET.C14NWriterTarget(write, *, with_comments=False, strip_text=False, ...)

It is the engine behind canonicalize(): useful when the document is large and building a tree just to canonicalize it is wasteful, or when output goes to a stream in pieces. write receives str, so list.append or a text file's write method fits, not a binary one. The options match canonicalize() (strip_text, with_comments and so on). Output is complete only after the parser's close().

Examples

import xml.etree.ElementTree as ET
out = []
p = ET.XMLParser(target=ET.C14NWriterTarget(out.append))
p.feed('<a z="1" a="2"/>')
p.close()
print(''.join(out))  # → <a a="2" z="1"></a>

See also

Open in the full glossarydocs.python.orgReport an inaccuracy