Install & Compatibility
Where this runs
tested against v10.0.0 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
17MB installed
● package 17MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
tinyxml2
✓ import tinyxml2
✗ from cmeel.prefix import tinyxml2
This quickstart demonstrates how to create a new XML document, add elements, attributes, and text, and then print its content. It also shows how to parse an existing XML string and navigate its elements, including checking for parsing errors using TinyXML-2's error handling API.
import tinyxml2
import os
# Create a new document
doc = tinyxml2.XMLDocument()
# Add a declaration (optional but good practice)
doc.InsertEndChild(doc.NewDeclaration("xml version=\"1.0\" encoding=\"UTF-8\""))
# Create a root element
root = doc.NewElement("Root")
doc.InsertEndChild(root)
# Add a child element with an attribute and text
child = doc.NewElement("Item")
child.SetAttribute("id", 123)
child.SetText("Example Text")
root.InsertEndChild(child)
# Add another child
another_child = doc.NewElement("SubItem")
another_child.SetText("More data")
child.InsertEndChild(another_child)
# Print the document to stdout
printer = tinyxml2.XMLPrinter()
doc.Print(printer)
print("\n--- Generated XML ---")
print(printer.CStr())
# Example of parsing an XML string
xml_string = "<data><item value='parsed_value'/></data>"
parsed_doc = tinyxml2.XMLDocument()
parsed_doc.Parse(xml_string)
print("\n--- Parsed XML Content ---")
if not parsed_doc.Error():
root_element = parsed_doc.FirstChildElement("data")
if root_element:
item_element = root_element.FirstChildElement("item")
if item_element:
print(f"Parsed item value: {item_element.Attribute('value')}")
else:
print("Item element not found.")
else:
print("Data root element not found.")
elif parsed_doc.ErrorID() == tinyxml2.XML_NO_ERROR:
print("Parsing completed with no document errors, but might be empty.")
else:
print(f"Parsing error: {parsed_doc.ErrorStr()} (ID: {parsed_doc.ErrorID()})")
# Example of saving to a file (optional, requires write access)
# output_filename = "output.xml"
# if os.environ.get('ENABLE_FILE_WRITE', '0') == '1': # Use env var for safety
# if doc.SaveFile(output_filename):
# print(f"XML successfully saved to {output_filename}")
# else:
# print(f"Error saving XML to {output_filename}: {doc.ErrorStr()}")
Debug
Known issues
gotchaTinyXML-2 uses a C++-style error handling approach where methods return status codes or require explicit checks via `XMLDocument.Error()` or `XMLElement.Error()` after operations. Unlike Pythonic exceptions, you must explicitly check for errors, especially after parsing (`Parse`, `LoadFile`) or file operations (`SaveFile`).fixAlways check `doc.Error()` or `element.Error()` and `doc.ErrorStr()`/`ErrorID()` after operations that might fail (e.g., `Parse`, `LoadFile`, `SaveFile`, navigating non-existent elements).
affects: All versions
gotchaWhen loading or saving files using `LoadFile` and `SaveFile`, ensure the provided paths are valid and that your application has the necessary read/write permissions. File I/O errors will set the document's internal error state.fixVerify file paths and permissions. Check `doc.Error()` after `LoadFile`/`SaveFile` to understand any I/O issues. Consider using `os.path.exists` or `try-except` blocks for general file system interactions.
affects: All versions
breakingUpgrading `cmeel-tinyxml2` might implicitly upgrade the underlying TinyXML-2 C++ library. While `cmeel` aims for stable bindings, major version bumps in TinyXML-2 (or `cmeel-tinyxml2`) could introduce API changes or subtle behavioral differences that break existing code.fixReview the upstream TinyXML-2 release notes and `cmeel-tinyxml2` GitHub repository for API changes when upgrading to a new major version. Thoroughly test your XML processing logic after an upgrade.
affects: Any major version transition (e.g., 9.x to 10.x)
Upgrade
Version history
11.0.0latest on PyPI · released May 21, 2026
Audit
Dependencies
No dependency data recorded yet.