xml · API

xml.document

xml.document — a small read-only tree built from XmlReader events.

API responses are small and are read by path: "every reservationSet/item/instancesSet/item, and under each its instanceType". That is awkward over a pull parser and natural over a tree, so Document builds one — but flat, as parallel arrays indexed by node id, rather than as recursive structs. A node is an element; its text is the concatenation of its direct text children (whitespace-only runs between child elements are dropped), which is the only text model an API response needs.

var doc = Document.parse(body)
for item in doc.find_all(doc.root(), "reservationSet/item/instancesSet/item"):
    print(doc.child_text(item, "instanceId"), doc.find_text(item, "instanceState/name"))

Path segments match the local name, so a namespace prefix on the document side never has to be spelled out. * matches any element.

Structs

Document

struct Document

Implements Movable, Sized

__init__

def __init__(out self)

parse

def parse(doc: String) -> Self

Parses a whole document. Raises on malformed XML or no root.

__len__

def __len__(self) -> Int

Number of elements in the document.

root

def root(self) -> Int

name

def name(self, node: Int) -> String

The element's name as written, prefix included.

text

def text(self, node: Int) -> String

The element's own text, entity-decoded.

parent

def parent(self, node: Int) -> Int

attr

def attr(self, node: Int, name: String, default: String = "") -> String

children

def children(self, node: Int) -> List[Int]

child

def child(self, node: Int, name: String) -> Int

The first child with local name name, or NO_NODE.

child_text

def child_text(self, node: Int, name: String, default: String = "") -> String

find_all

def find_all(self, node: Int, path: String) -> List[Int]

Every element reached from node by the /-separated path.

Each segment steps down one level and matches all children of that name, so a/b yields every b under every a, in document order.

find

def find(self, node: Int, path: String) -> Int

The first element find_all would return, or NO_NODE.

find_text

def find_text(self, node: Int, path: String, default: String = "") -> String

Aliases

comptime NO_NODE = -1