et-xmlfile

raw JSON →
2.0.0 verified Tue May 12 auth: no python install: verified quickstart: verified

A low-memory library for creating large XML files, implementing lxml's xmlfile module for the standard library. Current version: 2.0.0, released on October 25, 2024. Maintained by CharlieX. Requires Python 3.8 or higher. Release cadence: approximately every 3 years.

pip install et-xmlfile
error ModuleNotFoundError: No module named 'et_xmlfile'
cause The `et-xmlfile` library is not installed in the Python environment, or the Python interpreter cannot find it due to an incorrect environment setup or path issue.
fix
Install the library using pip: pip install et-xmlfile
error AttributeError: 'xmlfile' object has no attribute 'tag'
cause This error occurs when attempting to access an attribute like 'tag' (which belongs to an `Element` object from `xml.etree.ElementTree`) directly on the `et_xmlfile.xmlfile` object, mistakenly treating the file writer as an XML element.
fix
Ensure you are calling methods like write() or element() on the xmlfile object for streaming, and manipulate xml.etree.ElementTree.Element objects for their attributes (like tag or text) before writing them.
error TypeError: write() argument must be str, not bytes
cause The `write()` method of the `xmlfile` object, or an underlying file object, received an argument of an incorrect type (e.g., bytes instead of a string or an `Element` object), or vice-versa depending on the context manager's stream type.
fix
Ensure that the argument passed to write() is an xml.etree.ElementTree.Element instance as shown in examples, or a string if writing raw XML data, and matches the expected type of the underlying output stream (e.g., BytesIO expects bytes, regular file objects might expect strings).
gotcha Using the .element() method on the xmlfile context manager may negatively affect performance. It's recommended to create Elements and write them directly.
fix Avoid using the .element() method; instead, create Elements and write them directly.
gotcha Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead.
fix Avoid running pip as the 'root' user; instead, use a virtual environment for package installation or run pip as a non-root user.
gotcha Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager, potentially rendering your system unusable.
fix It is recommended to use a virtual environment instead (https://pip.pypa.io/warnings/venv). Alternatively, use the --root-user-action option if you understand the implications and wish to suppress this warning.
python os / libc status wheel install import disk
3.10 alpine (musl) - - 0.05s 17.9M
3.10 slim (glibc) - - 0.04s 18M
3.11 alpine (musl) - - 0.06s 19.7M
3.11 slim (glibc) - - 0.04s 20M
3.12 alpine (musl) - - 0.07s 11.6M
3.12 slim (glibc) - - 0.07s 12M
3.13 alpine (musl) - - 0.06s 11.2M
3.13 slim (glibc) - - 0.05s 12M
3.9 alpine (musl) - - 0.05s 17.4M
3.9 slim (glibc) - - 0.04s 18M

A minimal example demonstrating how to use et-xmlfile to create an XML file in memory.

from io import BytesIO
from xml.etree.ElementTree import Element
from et_xmlfile import xmlfile

out = BytesIO()
with xmlfile(out) as xf:
    el = Element('root')
    xf.write(el)

assert out.getvalue() == b'<root />'