larzxml
Ergonomic XML builder + an XXE-safe parser — zero dependencies.
The standard library makes two XML tasks harder than they should be: building XML reads clumsily with ElementTree, and parsing untrusted XML is dangerous by default (XXE and billion-laughs attacks). larzxml fixes both — a fluent builder that escapes correctly, and a parser that refuses DTDs and entities, so external-entity and exponential-expansion attacks simply can't happen.
Install
pip install larzxml
Use
from larzxml import element, parse
root = element("order", id="42")
root.sub("item", text="Widget", sku="A1")
root.sub("item", text="Gadget & <friends>", sku="B2") # escaped safely
print(root.to_xml(pretty=True))
tree = parse(xml_text) # safe by default
tree.find("item").text
tree.findall("item")
Safe by construction
parse('<!DOCTYPE x [<!ENTITY e SYSTEM "file:///etc/passwd">]><x>&e;</x>')
# -> XMLError: DTD/DOCTYPE is not allowed (XXE-safe parser)
DTDs and entity declarations are rejected, so both XXE (reading local files / SSRF) and billion-laughs (exponential entity expansion) are impossible — you don't have to remember to harden anything.
Tests
python -m unittest discover -s tests -v # 14 tests (incl. XXE + billion-laughs)
The Larz stack
One of 60+ pure-Python, zero-dependency libraries at github.com/larz-scripter. The XML sibling of larzhtml (XSS-safe HTML) and larzjson.
License
MIT (c) larz-scripter
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 larzxml-0.1.0.tar.gz.
File metadata
- Download URL: larzxml-0.1.0.tar.gz
- Upload date:
- Size: 5.9 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
6f006cf3fefa6cc9325efef4b9be10b18d6fc15de4c23bb1803110fbecd6e910
|
|
| MD5 |
c3d973ccb455ed1ae0b143506a4b1c52
|
|
| BLAKE2b-256 |
2da374fad2e3cff62055faf258a26d6d0cee9b63a64cb8f234cbf642a4b38c98
|
File details
Details for the file larzxml-0.1.0-py3-none-any.whl.
File metadata
- Download URL: larzxml-0.1.0-py3-none-any.whl
- Upload date:
- Size: 5.5 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via:
twine/7.0.0 CPython/3.12.13
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
3a8cdba5ac745aea009491c08dfb33f6f8854338ac9721032a7a7e1a5b304ccf
|
|
| MD5 |
57eac390f6dd69809a40221d872ba82e
|
|
| BLAKE2b-256 |
649cf4b7b53806c3f4d8b581f6589af4d75a018ad9fd0466fdc9215acab40233
|