Skip to content

Xml Parser

Spice
import "std/text/xml-parser";

XmlNode struct

Represents a node in an XML document.

Element nodes carry a tag name, ordered attributes and ordered child nodes (which may themselves be elements or text). Text nodes carry only the decoded character data. Children are owned as heap pointers so the parsed tree is reached via XmlNode* (heap on the root).

The parser handles a useful subset of XML 1.0: elements with attributes, self-closing tags, character data with the standard predefined entities (& < > " ') and numeric character references (&#NN; and &#xNN;), CDATA sections, comments and an optional XML declaration / processing instructions (which are skipped). DTDs, namespaces and external entities are not interpreted.

Constructors

ctor

Spice
public p XmlNode.ctor()

Construct an empty XML element node

Methods

getKind

Spice
public f<XmlNodeKind> XmlNode.getKind()

Retrieve the kind of the XML node

Returns: XmlNodeKind — Kind of the node

isElement

Spice
public f<bool> XmlNode.isElement()

Check whether the node is an element node

Returns: bool — true if the node is an element, false otherwise

isText

Spice
public f<bool> XmlNode.isText()

Check whether the node is a text node

Returns: bool — true if the node is text, false otherwise

getName

Spice
public f<const String&> XmlNode.getName()

Retrieve the tag name of the element. Panics if the node is not an element.

Returns: const String& — Tag name

getText

Spice
public f<const String&> XmlNode.getText()

Retrieve the character data of the text node. Panics if the node is not a text node.

Returns: const String& — Text content

getAttributeCount

Spice
public f<unsigned long> XmlNode.getAttributeCount()

Retrieve the number of attributes on the element. Panics if the node is not an element.

Returns: unsigned long — Number of attributes

hasAttribute

Spice
public f<bool> XmlNode.hasAttribute(string name)

Check whether the element has an attribute with the given name

Parameters

Name Type Description
name string Attribute name to look for

Returns: bool — true if the attribute exists, false otherwise

getAttribute

Spice
public f<const String&> XmlNode.getAttribute(string name)

Retrieve the value of the attribute with the given name. Panics if the node is not an element or the attribute does not exist.

Parameters

Name Type Description
name string Attribute name to look up

Returns: const String& — Attribute value

getChildCount

Spice
public f<unsigned long> XmlNode.getChildCount()

Retrieve the number of child nodes. Panics if the node is not an element.

Returns: unsigned long — Number of child nodes

getChild

Spice
public f<XmlNode*> XmlNode.getChild(unsigned long idx)

Retrieve the child node at the given index. Panics if the node is not an element.

Parameters

Name Type Description
idx unsigned long Index of the child

Returns: XmlNode* — Pointer to the child node

getChild

Spice
public f<XmlNode*> XmlNode.getChild(unsigned int idx)

Retrieve the child node at the given index. Panics if the node is not an element.

Parameters

Name Type Description
idx unsigned int Index of the child

Returns: XmlNode* — Pointer to the child node

findChild

Spice
public f<XmlNode*> XmlNode.findChild(string name)

Find the first child element with the given tag name. Returns nil if none exists. Text-node children are ignored.

Parameters

Name Type Description
name string

Returns: XmlNode*

hasChild

Spice
public f<bool> XmlNode.hasChild(string name)

Check whether the element has a direct child element with the given tag name

Parameters

Name Type Description
name string Tag name to look for

Returns: bool — true if such a child exists, false otherwise

getInnerText

Spice
public f<String> XmlNode.getInnerText()

Concatenated text of all direct text-node children, in document order. Useful for elements like <title>Hello</title> where the natural value is the text content.

Returns: String

serialize

Spice
public f<String> XmlNode.serialize()

Serializes this node into a compact XML string. Elements without children are written as self-closing tags. Text content and attribute values are escaped with the predefined entities, so the result round-trips through parseXml.

Returns: String — Compact XML representation

serializePretty

Spice
public f<String> XmlNode.serializePretty()

Serializes this node into a human-readable XML string. Elements that contain child elements are laid out with two-space indentation, one node per line; elements containing only text are kept on a single line.

Returns: String — Pretty-printed XML representation

XmlParser struct

Recursive-descent XML parser. Use parseXml for one-shot parsing.

Constructors

ctor

Spice
public p XmlParser.ctor(const String& input)

Construct an XML parser over the given input string

Parameters

Name Type Description
input const String& XML text to parse

Methods

parse

Spice
public f<Result<XmlNode*>> XmlParser.parse()

Parse the input string into an XML node tree

Returns: Result<XmlNode*> — Result holding the root XML node, or an error if the input is malformed

Functions

parseXml

Spice
public f<Result<XmlNode*>> parseXml(const String& input)

Parse an XML document and return the root element.

On success returns the root XmlNode (heap-allocated; the caller owns it). On failure returns an error describing the first problem encountered.

Parameters

Name Type Description
input const String&

Returns: Result<XmlNode*>

XmlNodeKind enum

Tag for the variant stored inside an XmlNode.

Item Value Description
XML_ELEMENT
XML_TEXT