{"id":7336,"library":"jxmlease","title":"jxmlease","description":"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.","status":"active","version":"1.0.3","language":"en","source_language":"en","source_url":"https://github.com/Juniper/jxmlease","tags":["xml","parsing","serialization","data structures","elementtree"],"install":[{"cmd":"pip install jxmlease","lang":"bash","label":"Install latest version"}],"dependencies":[{"reason":"Optional dependency for enhanced namespace handling and ElementTree parsing; standard ElementTree (built-in) is sufficient for basic functionality.","package":"lxml","optional":true}],"imports":[{"note":"The top-level `parse` function is a convenient shortcut for `jxmlease.Parser().parse()`.","wrong":"import jxmlease; jxmlease.Parser().parse(xml_string)","symbol":"parse","correct":"from jxmlease import parse"},{"note":"The top-level `emit_xml` function was introduced in v1.0.1 for direct conversion from Python data structures to XML, simplifying output.","wrong":"import jxmlease; jxmlease.XMLDictNode(data_structure).emit_xml()","symbol":"emit_xml","correct":"from jxmlease import emit_xml"},{"note":"Used for converting Python dictionaries to jxmlease's intelligent XML dictionary nodes for more control.","symbol":"XMLDictNode","correct":"from jxmlease import XMLDictNode"}],"quickstart":{"code":"import jxmlease\n\nxml_string = \"\"\"<root><item id=\"1\">Value 1</item><item id=\"2\">Value 2</item></root>\"\"\"\n\n# Parse XML to Python data structure\ndata_structure = jxmlease.parse(xml_string)\nprint(\"Parsed Data Structure:\")\nprint(data_structure.prettyprint())\n\n# Accessing data and attributes\nprint(f\"\\nFirst item value: {data_structure['root']['item'][0].value}\")\nprint(f\"First item ID: {data_structure['root']['item'][0].get_xml_attr('id')}\")\n\n# Convert Python data structure to XML\npython_data = {\n    'catalog': {\n        'book': [\n            {'@id': 'bk101', 'author': 'Gambardella, Matthew', 'title': 'XML Developer's Guide'},\n            {'@id': 'bk102', 'author': 'Ralls, Kim', 'title': 'Midnight Rain'}\n        ]\n    }\n}\n\n# Using emit_xml (available since v1.0.1)\noutput_xml = jxmlease.emit_xml(python_data)\nprint(\"\\nEmitted XML:\")\nprint(output_xml)","lang":"python","description":"This quickstart demonstrates parsing an XML string into a Python data structure, accessing elements and attributes, and converting a Python dictionary back into an XML string using the `parse` and `emit_xml` top-level functions. For more granular control over XML node creation, `XMLDictNode` and `XMLListNode` can be used directly."},"warnings":[{"fix":"Install `lxml` (`pip install lxml`) and `jxmlease` will automatically leverage its advanced functionality for improved namespace handling.","message":"Standard library's ElementTree does not maintain original XML namespace identifiers. If namespace fidelity is critical during parsing and iteration, consider installing `lxml`.","severity":"gotcha","affected_versions":"All versions"},{"fix":"For applications where child order of mixed tags is strictly semantic, `jxmlease` might require post-processing of the Python structure or a different XML library. Or, ensure consistent tag usage for siblings if order is important.","message":"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.","severity":"gotcha","affected_versions":"All versions, specifically 1.0.1 through 1.0.3"},{"fix":"This is an open issue. For critical use cases requiring perfectly formatted namespace output, manual post-processing of the XML string may be necessary, or consider alternatives for XML emission if this behavior is observed.","message":"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.","severity":"gotcha","affected_versions":"All versions, specifically 1.0.1 through 1.0.3"}],"env_vars":null,"last_verified":"2026-04-16T00:00:00.000Z","next_check":"2026-07-15T00:00:00.000Z","problems":[{"fix":"Use `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: ...`.","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.","error":"KeyError: 'attribute_name'"},{"fix":"Always 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.","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.","error":"AttributeError: If the node is out of date."}]}