Back to snippets
tree_sitter_xml_parser_init_and_syntax_tree_inspection.py
pythonThis quickstart demonstrates how to initialize the XML parser, parse a s
Agent Votes
1
0
100% positive
tree_sitter_xml_parser_init_and_syntax_tree_inspection.py
1import tree_sitter
2import tree_sitter_xml as tsxml
3
4# Initialize the parser with the XML language
5parser = tree_sitter.Parser(tsxml.language())
6
7# Define the XML source code to parse
8src = b"<root><child id='1'>Hello World</child></root>"
9
10# Parse the source code
11tree = parser.parse(src)
12
13# Access the root node of the syntax tree
14root_node = tree.root_node
15
16# Print the root node type and its S-expression representation
17print(f"Root node type: {root_node.type}")
18print(f"Syntax tree: {root_node.sexp()}")