cheatah
Module

parsers::xml

Functions

fn Document parse(std::string_view xml) source#

Parse xml into a Document (slab DOM).

Never throws.

The returned document always has at least the root node (root). Malformed input is tolerated: unterminated tags/comments/CDATA consume to end-of-input, and a </x> with no matching open element is ignored rather than aborting the parse.

Parameters
xml

the document text.

Returns

the parsed document (root-only for empty/blank input).

Complexity

O(n) time in the length of xml.

Allocation

the node slab and its strings on the heap (owned by the returned Document).

fn int root(const Document &doc) source#

The document root's node id (its children are the top-level nodes).

Complexity

O(1).

Allocation

none.

Parameters
doc

the document.

Returns

the root node id (its children are the top-level nodes).

fn bool is_element(const Document &doc, int id) source#

Whether id is a valid element node in doc.

Complexity

O(1).

Allocation

none.

Parameters
doc

the document.

id

the node id to test.

Returns

true iff id is a valid element node.

fn std::string tag(const Document &doc, int id) source#

The tag name of element id, or "" if id is not an element.

Complexity

O(1).

Allocation

one result string.

Parameters
doc

the document.

id

the element node id.

Returns

the tag name, or "" if id is not an element.

fn std::string attr(const Document &doc, int id, std::string_view name) source#

Value of attribute name on element id, or "" if absent (or id is not an element).

Complexity

O(k) in the attribute count of id.

Allocation

one result string.

Parameters
doc

the document.

id

the element node id.

name

the attribute name.

Returns

the attribute value, or "" if absent.

fn bool has_attr(const Document &doc, int id, std::string_view name) source#

Whether element id carries an attribute named name.

Complexity

O(k) in the attribute count of id.

Allocation

none.

Parameters
doc

the document.

id

the element node id.

name

the attribute name.

Returns

whether id carries attribute name.

fn std::string text(const Document &doc, int id) source#

The concatenated text of node id: for a text node, its own text; for an element, all descendant text in document order (like an XML .textContent).

Complexity

O(m) in the number of descendants of id.

Allocation

one result string.

Parameters
doc

the document.

id

the node id.

Returns

the concatenated descendant text.

fn std::vector< int > children(const Document &doc, int id) source#

The child node ids of id, in document order (elements and text).

Complexity

O(1) (returns a copy of the id list).

Allocation

the id list.

Parameters
doc

the document.

id

the node id.

Returns

the child node ids in document order.

fn int find(const Document &doc, int id, std::string_view tag) source#

The id of the first child element of id whose tag equals tag, or -1 if none.

(Direct children only — not descendants; use iter for the whole subtree.)

Complexity

O(c) in the direct-child count of id.

Allocation

none.

Parameters
doc

the document.

id

the parent node id.

tag

the child tag to match.

Returns

the first matching child element id, or -1.

fn std::vector< int > findall(const Document &doc, int id, std::string_view tag) source#

The ids of all direct child elements of id whose tag equals tag, in order.

Complexity

O(c) in the direct-child count of id.

Allocation

the result id list.

Parameters
doc

the document.

id

the parent node id.

tag

the child tag to match.

Returns

the matching direct-child element ids.

fn std::vector< int > iter(const Document &doc, int id, std::string_view tag) source#

The ids of every element in the subtree rooted at id (including id itself) whose tag equals tag, in document order — the analogue of an XML tree .iter(tag).

Complexity

O(m) in the subtree size of id.

Allocation

the result id list (and a transient walk stack).

Parameters
doc

the document.

id

the subtree root node id.

tag

the tag to match.

Returns

the matching element ids in the subtree, in document order.