jxmlease
jxmlease is a Python library designed for seamless conversion between XML documents and intelligent Python data structures. It aims to simplify XML processing by representing XML data and its metadata (like attributes) using extended Python list, dictionary, and string types. The current version is 1.0.3, with releases focusing on bug fixes and minor enhancements.
Common errors
-
KeyError: 'attribute_name'
cause Attempting to access an XML attribute directly using `node.get_xml_attr('attribute_name')` when the attribute does not exist on the node, and no default value was provided.fixUse `node.get_xml_attr('attribute_name', default_value)` to provide a fallback, or check for attribute existence before accessing: `if 'attribute_name' in node.xml_attrs: ...`. -
AttributeError: If the node is out of date.
cause Trying to modify an `XMLNodeBase` instance (e.g., `append_cdata()`, `delete_xml_attr()`, `set_xml_attr()`) that is no longer a valid, current reference within the parsed XML tree, typically after other structural modifications have occurred.fixAlways operate on the most recently returned or explicitly retrieved `XMLNodeBase` object. If modifications are intertwined, use `node.get_current_node()` to ensure you have an up-to-date reference before applying further changes.
Warnings
- gotcha Standard library's ElementTree does not maintain original XML namespace identifiers. If namespace fidelity is critical during parsing and iteration, consider installing `lxml`.
- gotcha When parsing XML with mixed element types that are expected to maintain a specific order (e.g., `<foo><a>one</a><b>two</b><a>three</a></foo>`), `jxmlease` might reorder siblings of different tags during conversion to Python dicts, leading to incorrect XML output if re-emitted. This is due to its dictionary-centric representation.
- gotcha XML output generated from Python data structures with explicit namespaces (e.g., `{'soapenv:Envelope xmlns:foo':'...'}`) may incorrectly include namespace declarations on *both* opening and closing tags, which is not standard compliant and can break other XML parsers.
Install
-
pip install jxmlease
Imports
- parse
import jxmlease; jxmlease.Parser().parse(xml_string)
from jxmlease import parse
- emit_xml
import jxmlease; jxmlease.XMLDictNode(data_structure).emit_xml()
from jxmlease import emit_xml
- XMLDictNode
from jxmlease import XMLDictNode
Quickstart
import jxmlease
xml_string = """<root><item id="1">Value 1</item><item id="2">Value 2</item></root>"""
# Parse XML to Python data structure
data_structure = jxmlease.parse(xml_string)
print("Parsed Data Structure:")
print(data_structure.prettyprint())
# Accessing data and attributes
print(f"\nFirst item value: {data_structure['root']['item'][0].value}")
print(f"First item ID: {data_structure['root']['item'][0].get_xml_attr('id')}")
# Convert Python data structure to XML
python_data = {
'catalog': {
'book': [
{'@id': 'bk101', 'author': 'Gambardella, Matthew', 'title': 'XML Developer's Guide'},
{'@id': 'bk102', 'author': 'Ralls, Kim', 'title': 'Midnight Rain'}
]
}
}
# Using emit_xml (available since v1.0.1)
output_xml = jxmlease.emit_xml(python_data)
print("\nEmitted XML:")
print(output_xml)