Xml Parser¶
| Spice | |
|---|---|
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 | |
|---|---|
Construct an empty XML element node
Methods¶
getKind¶
| Spice | |
|---|---|
Retrieve the kind of the XML node
Returns: XmlNodeKind — Kind of the node
isElement¶
| Spice | |
|---|---|
Check whether the node is an element node
Returns: bool — true if the node is an element, false otherwise
isText¶
| Spice | |
|---|---|
Check whether the node is a text node
Returns: bool — true if the node is text, false otherwise
getName¶
| Spice | |
|---|---|
Retrieve the tag name of the element. Panics if the node is not an element.
Returns: const String& — Tag name
getText¶
| Spice | |
|---|---|
Retrieve the character data of the text node. Panics if the node is not a text node.
Returns: const String& — Text content
getAttributeCount¶
| Spice | |
|---|---|
Retrieve the number of attributes on the element. Panics if the node is not an element.
Returns: unsigned long — Number of attributes
hasAttribute¶
| Spice | |
|---|---|
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 | |
|---|---|
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 | |
|---|---|
Retrieve the number of child nodes. Panics if the node is not an element.
Returns: unsigned long — Number of child nodes
getChild¶
| Spice | |
|---|---|
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 | |
|---|---|
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 | |
|---|---|
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 | |
|---|---|
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 | |
|---|---|
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 | |
|---|---|
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 | |
|---|---|
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 | |
|---|---|
Construct an XML parser over the given input string
Parameters
| Name | Type | Description |
|---|---|---|
input |
const String& |
XML text to parse |
Methods¶
parse¶
| Spice | |
|---|---|
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 | |
|---|---|
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 |