Skip to main content

No project description provided

Project description

myxmltools

Python PyPI Build Style

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 index idx.

  • getValues(xml, label) Returns a list with text content for all matching tags.

  • getElement(xml, label, el=0) Returns the matching DOM element at index el.

  • getAttribute(xml, label, attribute, idx=0) Returns the value of attribute from the selected tag.

  • tagExists(xml, label) Returns True if at least one matching tag exists, otherwise False.

Writing

  • updateXML(xml, label, value, idx=0) Updates text content for an existing tag at index idx.

  • updateOrCreateXML(xml, label, value, idx=0) Updates the tag at index idx if it exists. If idx is 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.Document and xml.dom.minidom.Element.

Contributing

  1. Fork the repository and create a feature branch.
  2. Install development dependencies:
poetry install --with test,devel
  1. Run tests:
poetry run pytest
  1. Run lint checks:
poetry run ruff check .
  1. 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 updateOrCreateXML support for append-at-tail behavior.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

myxmltools-0.2.0.tar.gz (3.4 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

myxmltools-0.2.0-py3-none-any.whl (4.7 kB view details)

Uploaded Python 3

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

Hashes for myxmltools-0.2.0.tar.gz
Algorithm Hash digest
SHA256 da931099846fed4214d95c77a76fd6384c6ed2f9b8b9c9b2ccb0d04e80578abb
MD5 a7402f753456eb6fef837b8495937f83
BLAKE2b-256 ebf4850f366ebba615278b16c7585e0a173e5fcc6f7eeee8fa112d4a9dfd6d69

See more details on using hashes here.

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

Hashes for myxmltools-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 39f451b34276534832f368bb5d97291db637537a583b3e222202c1f7657f4be0
MD5 5db201a8db297ad2e4a3bdb2f251e15d
BLAKE2b-256 676f41d4c3a2ecffb15f697920e9b9145d9ea70abba3d95015150fc3eb292603

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page