MCPcopy Index your code
hub / github.com/python/cpython / write

Method write

Lib/xml/etree/ElementTree.py:687–740  ·  view source on GitHub ↗

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

(self, file_or_filename,
              encoding=None,
              xml_declaration=None,
              default_namespace=None,
              method=None, *,
              short_empty_elements=True)

Source from the content-addressed store, hash-verified

685 return self._root.iterfind(path, namespaces)
686
687 def write(self, file_or_filename,
688 encoding=None,
689 xml_declaration=None,
690 default_namespace=None,
691 method=None, *,
692 short_empty_elements=True):
693 """Write element tree to a file as XML.
694
695 Arguments:
696 *file_or_filename* -- file name or a file object opened for writing
697
698 *encoding* -- the output encoding (default: US-ASCII)
699
700 *xml_declaration* -- bool indicating if an XML declaration should be
701 added to the output. If None, an XML declaration
702 is added if encoding IS NOT either of:
703 US-ASCII, UTF-8, or Unicode
704
705 *default_namespace* -- sets the default XML namespace (for "xmlns")
706
707 *method* -- either "xml" (default), "html, "text", or "c14n"
708
709 *short_empty_elements* -- controls the formatting of elements
710 that contain no content. If True (default)
711 they are emitted as a single self-closed
712 tag, otherwise they are emitted as a pair
713 of start/end tags
714
715 """
716 if self._root is None:
717 raise TypeError('ElementTree not initialized')
718 if not method:
719 method = "xml"
720 elif method not in _serialize:
721 raise ValueError("unknown method %r" % method)
722 if not encoding:
723 if method == "c14n":
724 encoding = "utf-8"
725 else:
726 encoding = "us-ascii"
727 with _get_writer(file_or_filename, encoding) as (write, declared_encoding):
728 if method == "xml" and (xml_declaration or
729 (xml_declaration is None and
730 encoding.lower() != "unicode" and
731 declared_encoding.lower() not in ("utf-8", "us-ascii"))):
732 write("<?xml version='1.0' encoding='%s'?>\n" % (
733 declared_encoding,))
734 if method == "text":
735 _serialize_text(write, self._root)
736 else:
737 qnames, namespaces = _namespaces(self._root, default_namespace)
738 serialize = _serialize[method]
739 serialize(write, self._root, qnames, namespaces,
740 short_empty_elements=short_empty_elements)
741
742 def write_c14n(self, file):
743 # lxml.etree compatibility. use output method instead

Calls 6

_get_writerFunction · 0.85
_serialize_textFunction · 0.85
_namespacesFunction · 0.85
serializeFunction · 0.85
writeFunction · 0.50
lowerMethod · 0.45