Something like this?

>>> from xml.sax.saxutils import escape
>>> escape("< & >")   
'&lt; &amp; &gt;'
Answer from mbarkhau on Stack Overflow
🌐
Python
wiki.python.org › moin › EscapingXml
Escaping XML - Python Wiki
November 15, 2008 - This can be used to add additional entities specific to the DTD of the document being generated, or to cause particular characters to be encoded as character references: 1 >>> escape("abc", {"b": "&#98;"}) 2 'a&#98;c' 3 >>> escape("My product, PyThingaMaJiggie, is really cool.", 4 ... {"PyThingaMaJiggie": "&productName;"}) 5 'My product, &productName;, is really cool.' The xml.sax.saxutils module provides an unescape() function as well.
🌐
Python
docs.python.org › 3 › library › xml.etree.elementtree.html
xml.etree.ElementTree — The ElementTree XML API
January 29, 2026 - Generates a string representation of an XML element, including all subelements. element is an Element instance. encoding [1] is the output encoding (default is US-ASCII). Use encoding="unicode" to generate a Unicode string (otherwise, a bytestring ...
🌐
Stuffaboutcode
stuffaboutcode.com › 2012 › 06 › python-encode-xml-escape-characters.html
<Stuff about="code" />: Python - encode XML escape characters
xmlText = encodeXMLText("Some text with escape characters & " ' < > you want to make xml safe") ... Note: only a member of this blog may post a comment. ... .net (4) Adventures in Minecraft (5) asp (3) c (5) c# (4) camera (7) Car (4) games (24) get_iplayer (5) gpio (13) gps (4) html (3) microbit ...
🌐
XML.com
xml.com › pub › a › 2002 › 11 › 13 › py-xml.html
Proper XML Output in Python
November 13, 2002 - Consider one of the earlier examples, with a slight change -- the word "degrees" has been replaced with the byte B0 (octal 260), which is the degree symbol in the popular ISO-8859-1 and Windows-1252 character encodings: $ python -i listing2.py >>> write_xml_cdata_log_entry(2, "In any triangle, each interior angle < 90\260") <entry level="ERROR" date="Tue Oct 22 06:33:51 2002"> In any triangle, each interior angle &lt; 90° </entry> >>>
🌐
The Hitchhiker's Guide to Python
docs.python-guide.org › scenarios › xml
XML parsing — The Hitchhiker's Guide to Python
from xmlschema import XMLSchema, etree_tostring # load a XSD schema file schema = XMLSchema("your_schema.xsd") # validate against the schema schema.validate("your_file.xml") # or schema.is_valid("your_file.xml") # decode a file data = schmema.decode("your_file.xml") # encode to string s = etree_tostring(schema.encode(data)) This opinionated guide exists to provide both novice and expert Python developers a best practice handbook to the installation, configuration, and usage of Python on a daily basis.
🌐
Python
docs.python.org › 3 › library › xml.sax.utils.html
xml.sax.saxutils — SAX Utilities
The resulting string can be used directly as an attribute value: >>> print("<element attr=%s>" % quoteattr("ab ' cd \" ef")) <element attr="ab ' cd &quot; ef"> This function is useful when generating attribute values for HTML or any SGML using ...
🌐
GitHub
gist.github.com › walkermatt › 440b2c1ad444e9fa7fd0aebdc6d44cad
Reading and writing a UTF-8 encoded XML file with Python ElementTree · GitHub
Writing involves writing the XML doc to UTF8 encoded bytes by calling ElementTree.tostring and specifying encoding="utf-8", then open the destination for writing in UTF8 and write the decoded bytes.
Find elsewhere
🌐
Virtualzero
blog.virtualzero.tech › entries › python › xml › how-to-encode-strings-for-xml-with-python
How to Encode Strings for XML with Python | VirtualZero Blog
March 22, 2020 - Stings within XML tags need to be properly formatted. Without proper formatting, a XML Parsing Error is likely to occur. This tutorial demonstrates how to encode strings properly for XML using Python.
🌐
O'Reilly
oreilly.com › library › view › python-cookbook › 0596001673 › ch12s11.html
Autodetecting XML Encoding - Python Cookbook [Book]
July 19, 2002 - Note that encoding_name might not have an installed decoder (e.g., EBCDIC) """ # A more efficient implementation would not decode the whole # buffer at once, but then we'd have to decode a character at # a time looking for the quote character, and that's a pain encoding = "utf_8" # According to the XML spec, this is the default # This code successively tries to refine the default: # Whenever it fails to refine, it falls back to # the last place encoding ...
Authors   Alex MartelliDavid Ascher
Published   2002
Pages   608
🌐
ActiveState
code.activestate.com › recipes › 303668-encoding-unicode-data-for-xml-and-html
Encoding Unicode data for XML and HTML « Python recipes « ActiveState Code
September 7, 2004 - The following code contains a trivial function "encode_for_xml" that illustrates the "xmlcharrefreplace" error handler, and a function "_xmlcharref_encode" which emulates "xmlcharrefreplace" for Python pre-2.3.
🌐
Real Python
realpython.com › python-xml-parser
A Roadmap to XML Parsers in Python – Real Python
September 25, 2023 - If you’re interested in how to encapsulate this logic in a Python class and take advantage of a generator for lazy evaluation, then expand the collapsible section below. ... import mmap import untangle class XMLTagStream: def __init__(self, path, tag_name, encoding="utf-8"): self.file = open(path) self.stream = mmap.mmap( self.file.fileno(), 0, access=mmap.ACCESS_READ ) self.tag_name = tag_name self.encoding = encoding self.start_tag = f"<{tag_name}>".encode(encoding) self.end_tag = f"</{tag_name}>".encode(encoding) def __enter__(self): return self def __exit__(self, *args, **kwargs): self.s
🌐
GitHub
gist.github.com › 3067859
Different variants of writting XML in Python · GitHub
Different variants of writting XML in Python · Raw · writexml.py · This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
🌐
W3Schools
w3schools.com › python › ref_string_encode.asp
Python String encode() Method
Python Examples Python Compiler Python Exercises Python Quiz Python Challenges Python Practice Problems Python Server Python Syllabus Python Study Plan Python Interview Q&A Python Bootcamp Python Certificate Python Training ... The encode() method encodes the string, using the specified encoding.
Top answer
1 of 3
5

The most elegant solution is certainly using the third-party library lxml, which is being used a lot – for good reasons. It offers both a pretty_print and an xml_declaration parameter in the tostring() method, so you get both. And the API is quite close to that of the std-lib ElementTree, which you seem to be using now. Here's an example:

>>> from lxml import etree
>>> doc = etree.parse(xmlPath)
>>> print etree.tostring(doc, encoding='UTF-8', xml_declaration=True,
                         pretty_print=True)
<?xml version='1.0' encoding='UTF-8'?>
<main>
  <sub>
    <name>Ana</name>
    <detail/>
    <type>smart</type>
  </sub>
</main>

However, I understand your desire to use the "included batteries" only. As far as I can see, xml.etree.ElementTree has no means of changing the indentation automatically. But the minidom work-around has a solution to getting both pretty-printing and a full declaration: use the encoding parameter of the toprettyxml() method!

>>> doc = minidom.parseString(ET.tostring(root))
>>> print doc.toprettyxml(encoding='utf8')
<?xml version="1.0" encoding="utf8"?>
<main>
    <sub>
        <name>Ana</name>
        <detail/>
        <type>smart</type>
    </sub>
</main>

(Be aware that the returned string is already encoded and that you should write it to a file opened in binary mode ("wb") and without further encoding.)

2 of 3
5
from xml.dom import minidom
xmlstr = minidom.parseString(ET.tostring(root)).toprettyxml(indent="   ", encoding='UTF-8')
with open(xmlPath, "w") as f:
    f.write(str(xmlstr.decode('UTF-8')))
    f.close()

Probably This will resolve your issue without using external libraries like lxml