Install & Compatibility
Where this runs
tested against v0.7.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
muslpy 3.10–3.910 runs
installs and imports cleanly · install 0.0s · import 0.000s · 67.7MB
glibcpy 3.10–3.910 runs
installs and imports cleanly · install 1.6s · import 0.000s · 19MB
66MB installed
● package 66MB
Code
Verified usage
Verified import paths — ran on the pinned version, not inferred.
language_xml
✓ from tree_sitter_xml import language_xml
✗ from tree_sitter_xml import language
language_dtd
✓ from tree_sitter_xml import language_dtd
This quickstart demonstrates how to load the XML grammar using `tree_sitter_xml.language()` and then parse a simple XML string using `tree_sitter.Parser`. It also shows how to print the S-expression of the parse tree and find specific elements.
import tree_sitter
from tree_sitter_xml import language
# Load the XML grammar
XML_LANGUAGE = language()
# Initialize the parser
parser = tree_sitter.Parser()
parser.set_language(XML_LANGUAGE)
# Sample XML string
xml_code = """
<root>
<item id="1">Value 1</item>
<item id="2">Value 2</item>
</root>
"""
# Parse the XML
tree = parser.parse(xml_code.encode('utf8'))
# Print the S-expression (a common way to inspect the parse tree)
print(f"Parsed XML Tree S-expression:\n{tree.root_node.sexp()}")
# Example of traversing a node (e.g., finding the 'item' elements)
root_node = tree.root_node
item_nodes = [child for child in root_node.children if child.type == 'element' and child.text.decode('utf8').strip().startswith('<item')]
print(f"\nFound {len(item_nodes)} 'item' elements.")
if item_nodes:
print(f"First item's text: {item_nodes[0].text.decode('utf8')}")
Debug
Known issues
gotcha`tree-sitter-xml` itself doesn't provide parsing functions directly. It exposes `language()` and `dtd_language()` to retrieve pre-compiled grammars, which must then be used with `tree_sitter.Parser` for actual parsing. You need both libraries.fixAlways import `tree_sitter.Parser` and set the language on its instance: `parser = tree_sitter.Parser(); parser.set_language(tree_sitter_xml.language())`.
affects: All
breakingUpdates to the underlying `tree-sitter` library (especially major versions or breaking changes in its Python bindings) may introduce API changes that could affect how `tree-sitter-xml`'s grammars interact with the parser.fixAlways test your parsing logic after `tree-sitter` upgrades. Consult the `tree-sitter` and `tree-sitter-xml` release notes.
affects: Potentially any `tree-sitter` major version bump (e.g., 0.x to 0.y if `tree-sitter-xml` doesn't update concurrently).
gotchaXML and DTD grammars are distinct. Use `tree_sitter_xml.language()` for parsing XML documents and `tree_sitter_xml.dtd_language()` for parsing DTD files; they are not interchangeable.fixEnsure you're using the correct grammar function (`language()` or `dtd_language()`) based on the file type you intend to parse.
affects: All
gotchaPre-compiled grammars (`.so`, `.dylib`, or `.dll` files) are platform-specific. While `tree-sitter-xml` aims to distribute compatible binaries, issues can arise in unusual environments (e.g., exotic OS, specific Python distributions) or if custom grammar compilation is attempted.fixIf parsing fails with 'Language not found' or similar errors despite correct imports, check platform compatibility. Ensure your `pip` environment matches the distributed binaries. For complex setups, consider building the grammar manually via `tree_sitter.Language.build_library` if provided by the grammar's source.
affects: All
Upgrade
Version history
0.7.0latest on PyPI · released Nov 13, 2024
Audit
Dependencies
tree-sitterrequiredThis library provides grammars; `tree-sitter` provides the core parsing engine that utilizes them.