You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
load("@xml.bzl", "xml")
doc=xml.parse("<root><a>1</a><b>2</b><c>3</c></root>")
root=xml.get_document_element(doc)
# Get all child elementsforchildinxml.get_child_elements(root):
print(xml.get_tag_name(child), "=", xml.get_text(child))
# Output: a = 1, b = 2, c = 3# Navigate to parentfirst_child=xml.get_first_child_element(root)
parent=xml.get_parent(first_child)
print(xml.get_tag_name(parent)) # "root"# Root element has no parentprint(xml.get_parent(root)) # None
Working with Attributes
load("@xml.bzl", "xml")
doc=xml.parse('<item id="123" type="widget" enabled="true"/>')
root=xml.get_document_element(doc)
# Get a specific attributeid=xml.get_attribute(root, "id") # "123"# Get with default valuecolor=xml.get_attribute(root, "color", "blue") # "blue" (default)# Check if attribute existsifxml.has_attribute(root, "enabled"):
print("Item is enabled")
# Get all attributes as a dictattrs=xml.get_attributes(root) # {"id": "123", "type": "widget", "enabled": "true"}
Finding Elements
load("@xml.bzl", "xml")
xml_str='''<root> <item id="first">One</item> <item id="second">Two</item> <nested> <item id="third">Three</item> </nested></root>'''doc=xml.parse(xml_str)
# Find all elements with a specific tag nameitems=xml.find_elements_by_tag_name(doc, "item") # Returns 3 items# Find first element with a tag namefirst_item=xml.find_element_by_tag_name(doc, "item")
print(xml.get_text(first_item)) # "One"# Find element by idsecond=xml.find_element_by_id(doc, "second")
print(xml.get_text(second)) # "Two"# Find elements by attributeenabled_items=xml.find_elements_by_attribute(doc, "enabled", "true")
The parser tracks errors encountered during parsing. By default, it operates in lenient mode, continuing to parse even when errors are found.
load("@xml.bzl", "xml")
# Lenient mode (default) - errors are tracked but parsing continuesdoc=xml.parse("<root><a></b></root>") # Mismatched tags# Check for errorsifxml.has_errors(doc):
forerrorinxml.get_errors(doc):
print("Error:", error.message)
print("Type:", error.type)
# Errors are also accessible directly on the documentforerrorindoc.errors:
print(error.type, "-", error.message)
Strict Mode
Use strict=True to fail immediately on any parsing error:
load("@xml.bzl", "xml")
# Strict mode - fails on first errordoc=xml.parse("<root><a></b></root>", strict=True)
# This will call fail() with error details
Error Types
The following error types are detected:
Error Type
Constant
Description
Mismatched tag
xml.ERROR_MISMATCHED_TAG
Closing tag doesn't match opening tag
Unclosed tag
xml.ERROR_UNCLOSED_TAG
Tag was never closed
Unexpected end tag
xml.ERROR_UNEXPECTED_END_TAG
Closing tag with no matching opener
Multiple root elements
xml.ERROR_MULTIPLE_ROOT_ELEMENTS
Document has more than one root element
Text outside root
xml.ERROR_TEXT_OUTSIDE_ROOT
Non-whitespace text outside root element
Error Object Fields
Each error object has the following fields:
type - The error type constant
message - Human-readable error description
Additional fields depending on error type (e.g., expected, found, tag_name, count)
Serialization
load("@xml.bzl", "xml")
doc=xml.parse('<root><child attr="value">text</child></root>')
# Serialize to string (nested elements are indented by default)xml_string=xml.to_string(doc)
# Use custom indentation (4 spaces instead of default 2)xml_string=xml.to_string(doc, indent_str=" ")
# Use tabs for indentationxml_string=xml.to_string(doc, indent_str="\t")
# Compact output (no indentation or extra whitespace)xml_string=xml.to_string(doc, pretty=False)