No project description provided
Project description
myxmltools
Small utility library to read and update XML nodes using xml.dom.minidom.
Installation
Requirements
- Python 3.10+
Option 1: Install from local source with pip
pip install .
Option 2: Use Poetry (development setup)
poetry install
Usage
import xml.dom.minidom as md
from myxmltools import (
getValue,
getValues,
getAttribute,
tagExists,
updateXML,
updateOrCreateXML,
setAttribute,
removeTag,
pretty_print,
)
doc = md.parseString(
"""
<root>
<name id="42">John Doe</name>
<age>30</age>
<name id="99">Jane Doe</name>
</root>
"""
)
root = doc.documentElement
# Read values
first_name = getValue(root, "name")
all_names = getValues(root, "name")
name_id = getAttribute(root, "name", "id", idx=1)
# Update and create
updateXML(root, "age", 31)
updateOrCreateXML(root, "name", "Mark", idx=2)
# Attributes and removal
setAttribute(root, "age", "unit", "years")
removeTag(root, "name", idx=0)
# Check existence and pretty print
exists = tagExists(root, "age")
print(pretty_print(doc))
Functions
Reading
-
getValue(xml, label)Returns the text content of the first matching tag. -
getFromXml(xml, label, idx=0)Returns the text content of the matching tag at indexidx. -
getValues(xml, label)Returns a list with text content for all matching tags. -
getElement(xml, label, el=0)Returns the matching DOM element at indexel. -
getAttribute(xml, label, attribute, idx=0)Returns the value ofattributefrom the selected tag. -
tagExists(xml, label)ReturnsTrueif at least one matching tag exists, otherwiseFalse.
Writing
-
updateXML(xml, label, value, idx=0)Updates text content for an existing tag at indexidx. -
updateOrCreateXML(xml, label, value, idx=0)Updates the tag at indexidxif it exists. Ifidxis exactly the next valid index, it appends a new tag. -
setAttribute(xml, label, attribute, value, idx=0)Sets an attribute on the selected tag. -
removeTag(xml, label, idx=0)Removes the selected tag from its parent node.
Formatting
pretty_print(dom)Returns an indented XML string with empty lines removed.
Notes
- Functions raise descriptive exceptions for invalid labels, indices, attributes, or node types.
- Most APIs accept both
xml.dom.minidom.Documentandxml.dom.minidom.Element.
Contributing
- Fork the repository and create a feature branch.
- Install development dependencies:
poetry install --with test,devel
- Run tests:
poetry run pytest
- Run lint checks:
poetry run ruff check .
- Open a pull request with a clear description of your change.
Changelog
0.2.0
- Added robust validation and error handling for tag/attribute access.
- Added indexed read and update operations for repeated tags.
- Added
updateOrCreateXMLsupport for append-at-tail behavior.
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file myxmltools-0.2.0.tar.gz.
File metadata
- Download URL: myxmltools-0.2.0.tar.gz
- Upload date:
- Size: 3.4 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.3.2 CPython/3.14.3 Linux/6.19.8-200.fc43.x86_64
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
da931099846fed4214d95c77a76fd6384c6ed2f9b8b9c9b2ccb0d04e80578abb
|
|
| MD5 |
a7402f753456eb6fef837b8495937f83
|
|
| BLAKE2b-256 |
ebf4850f366ebba615278b16c7585e0a173e5fcc6f7eeee8fa112d4a9dfd6d69
|
File details
Details for the file myxmltools-0.2.0-py3-none-any.whl.
File metadata
- Download URL: myxmltools-0.2.0-py3-none-any.whl
- Upload date:
- Size: 4.7 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/2.3.2 CPython/3.14.3 Linux/6.19.8-200.fc43.x86_64
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
39f451b34276534832f368bb5d97291db637537a583b3e222202c1f7657f4be0
|
|
| MD5 |
5db201a8db297ad2e4a3bdb2f251e15d
|
|
| BLAKE2b-256 |
676f41d4c3a2ecffb15f697920e9b9145d9ea70abba3d95015150fc3eb292603
|