minixml
Minimalist package for reading, writing, creating and editing an XML element tree.
Software
There are no dependencies except for the standard Python library.
This code has been lovingly hand-crafted. No AI tools were used in its development.
License
Installation
pip install minixml
Usage
import minixml
# Create the root element.
svg = minixml.Element("svg", width=320, height=130, xmlns="http://www.w3.org/2000/svg")
# A new element can be created from a parent element.
# It is automatically appended to the parent as a child.
svg.create(
"rect",
width=300,
height=100,
x=10,
y=10,
style="fill:rgb(0,0,255);stroke-width:3;stroke:red",
)
# An element can also be created from scratch and subsequently appended.
t = minixml.Element("text", x=5, y=15, fill="red")
t.append("I love SVG!")
svg.append(t)
# Serialize the Element tree into XML code; the XML declaration is prepended.
xmlcode = minixml.unparse(svg, indent=2)
# Ensure that unparse and parse are symmetric functions.
assert svg == minixml.parse(xmlcode)
# Display the result.
print(xmlcode)
Produces:
<?xml version="1.0" encoding="utf-8"?>
<svg width="320" height="130" xmlns="http://www.w3.org/2000/svg">
<rect width="300" height="100" x="10" y="10" style="fill:rgb(0,0,255);stroke-width:3;stroke:red"/>
<text x="5" y="15" fill="red">I love SVG!</text>
</svg>
API
The minixml namespace contains the features most client software will need:
These are automatically imported into minixml from minixml.element
and minixml.io, which modules in most cases will not have to be
imported directly by client software.
The following was generated using pydoc-markdown https://github.com/NiklasRosenstein/pydoc-markdown
minixml
Minimalist package for reading, writing, creating and editing an XML element tree.
minixml.element
Class to represent an XML element.
An instance of Element has a tag, attributes, children and possibly a parent. An instance can be part of at most one element tree. All operations should be performed using the methods, which will maintain the integrity of the references between instances.
Element Objects
class Element()
XML element having a tag, attributes, parent (if any) and children (if any).
__init__
def __init__(tag: str, *children, **attributes)
The instance is initialized as a root element (i.e. it has no parent).
__str__
def __str__()
Return the string representation of the element's starting tag.
__getitem__
def __getitem__(key: [int, str])
If 'key' is int, get the key'th child. If 'key' is str, get the value of the attribute.
__setitem__
def __setitem__(key: [int, str], value)
If 'key' is int, set the key'th child. Convert value to str if not Element. If 'key' is str, set the value (converted to str) of the attribute.
__delitem__
def __delitem__(key: [int, str])
If 'key' is int, delete the key'th child. If 'key' is str, delete the attribute.
__contains__
def __contains__(key: [str, Element])
If 'key' is an Element instance, is it among the children?, If 'key' is str, does this element have the given attribute?
__iter__
def __iter__()
Iterate over the children of this element.
__len__
def __len__()
Return the number of children of this element.
__eq__
def __eq__(other)
Are this element and the other equal, recursively? Ignores the parent.
__iadd__
def __iadd__(other: [str, Element])
Append the other element to the end of the list of children of this element.
tag
@property
def tag()
Return the tag of this element; read-only.
parent
@property
def parent()
Return the parent, if any, of this element; read-only.
attributes
@property
def attributes()
Return the dictionary of the attributes; read only. This is a shallow copy of the dictionary in the instance. Changing it will therefore not affect the attributes in the instance.
children
@property
def children()
Return the list of children; read only. This is a shallow copy of the list in the instance. Changing it will therefore not affect the list of children in the instance.
text
@property
def text()
Return the text content of this element. All children that are non-blank texts are concatenated.
root
@property
def root()
Return the root element of this element. May be self.
depth
@property
def depth()
Depth of hierarchy for this element; number of parent steps to root.
get
def get(key: str, default=None)
Get the value of the 'key' attribute, or return the default if not present.
set
def set(key: str, value)
Set the value of the attribute in this element.
index
def index(other)
Return the slot number for 'other' among the children of this element. Checks for element equality, not identity. Raise IndexError if not present.
append
def append(other: [str, Element])
Append the other element to the end of the list of children of this element.
insert
def insert(i: int, other: [str, Element])
Insert 'other' at position 'i' in the list of children of this element.
replace
def replace(i: int, other: [str, Element])
Replace the element at position 'i' in the list of children of this element with 'other'. The replaced element is returned.
pop
def pop(i: [int, None] = None)
Remove the element at position i (last if None) and return it.
free
def free()
Remove this element from its parent, if any. Return itself.
create
def create(tag: str, **attributes)
Create an element with the given tag and attributes, and append to the children of this element. Return the created element.
copy
def copy()
Make a copy of this element and its children (i.e. deepcopy). The returned copy element has no parent.
walk
def walk(test=None)
Walk over this element and all its Element children recursively, yielding those elements that match the given test function. If no test function given, yield all. NOTE: The test function must handle both Element instances and strings.
diff
def diff(other: Element)
Return the first difference between this element and the other, recursively. Ignores the parents of this element and the other. Return a tuple describing the difference, or None.
minixml.io
Parse XML code into an Element tree, and unparse an Element tree into XML code.
parse
def parse(content, element_class=Element)
Parse the given XML content (string). Return the root XML element.
read
def read(filepath_or_stream, element_class=Element)
Read and parse the file given by its path, or the open file object. Return the root Element.
unparse
def unparse(element, indent=0, decl='<?xml version="1.0" encoding="utf-8"?>')
Return the XML code for the element and its children as a string. 'indent' is the number of blanks to use for each indentation level. The 'decl' value is output first if it is a non-empty string.
write
def write(outfile,
element,
indent=0,
decl='<?xml version="1.0" encoding="utf-8"?>')
Write the XML of the element and its children into the open file object. The 'decl' value is output first if it is a non-empty string.
ContentHandler Objects
class ContentHandler(xml.sax.ContentHandler)
Handle XML SAX parse events, turning them into an Element tree.
Metadata
Release files for minixml 0.7.0
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | Provenance | Actions |
|---|---|---|---|---|
| minixml-0.7.0.tar.gz | 7.5 kB | Without provenance | Details for minixml-0.7.0.tar.gz |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Actions Reset |
|---|---|---|---|---|
| minixml-0.7.0-py3-none-any.whl | Python 3 | none | any | Details for minixml-0.7.0-py3-none-any.whl |
Total release size: 17.1 kB
Release files / minixml-0.7.0.tar.gz
| Download URL | minixml-0.7.0.tar.gz |
|---|---|
| Size | 7.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
540e423631d3e502a91c546e8e92d6145af506ba833c8472142860f672a845ee
|
|
BLAKE2b-256 checksum How to use checksums |
6478b9b4ef6759b38b916a04362fa08301e35c9b3ae5e0a6da13334722523357
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.21 {"installer":{"name":"uv","version":"0.12.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Release files / minixml-0.7.0-py3-none-any.whl
| Download URL | minixml-0.7.0-py3-none-any.whl |
|---|---|
| Size | 9.6 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
bc1a02b24bfe773ac4f735d78d10b584a4cb681cd0d6e012734889889cb72f42
|
|
BLAKE2b-256 checksum How to use checksums |
6bdb4377f15ed8c8528531f70c6b8d6af70e992cc8c29bb8e86129573b6d2c0d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
No |
| Uploaded via |
uv/0.12.21 {"installer":{"name":"uv","version":"0.12.21","subcommand":["publish"]},"python":null,"implementation":{"name":null,"version":null},"distro":{"name":"Ubuntu","version":"26.04","id":"resolute","libc":null},"system":{"name":null,"release":null},"cpu":null,"openssl_version":null,"setuptools_version":null,"rustc_version":null,"ci":null}
|
Security for minixml 0.7.0
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
No provenance found
None of the files in this release were uploaded with provenance metadata.
A note on package security
PyPI is a repository for distributing packages, not a service for auditing them. While third-party security researchers and our staff do their best to detect and remove malicious packages, PyPI cannot guarantee the quality, security or suitability of any given package.
To learn more about protecting yourself from malicious actors, visit our package security documentation.