Xml Node¶
| 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*.
Destroying a node recursively destroys everything below it, so a whole tree is released with a single deleteXmlNode call on its root. Accessors like getChild and findChild hand out borrowed pointers that stay owned by their parent, while addChild takes ownership of what it is given.
Note that the root cannot simply be held in a heap XmlNode* and left to the compiler: the heap qualifier only emits a raw deallocation when the variable goes out of scope and never runs the destructor of the pointee, so the children below the root would be lost.
Parsing lives in "std/text/xml/xml-parser" and serialization in "std/text/xml/xml-serializer", so programs only pull in what they use.
Constructors¶
ctor¶
| Spice | |
|---|---|
Construct an empty XML node of the given kind
Parameters
| Name | Type | Description |
|---|---|---|
kind |
XmlNodeKind |
Kind of the node (default: XmlNodeKind::XML_ELEMENT) |
dtor¶
| Spice | |
|---|---|
Destruct the XML node, releasing every child node and attribute it owns. Since the children are destroyed the same way, this tears down the whole subtree below the 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
setName¶
| Spice | |
|---|---|
Set the tag name of the element. Panics if the node is not an element.
Parameters
| Name | Type | Description |
|---|---|---|
name |
const String& |
New 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
setText¶
| Spice | |
|---|---|
Set the character data of the text node. Panics if the node is not a text node.
Parameters
| Name | Type | Description |
|---|---|---|
text |
const String& |
New 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
getAttributeKey¶
| Spice | |
|---|---|
Retrieve the name of the attribute at the given index, in document order. Panics if the node is not an element.
Parameters
| Name | Type | Description |
|---|---|---|
idx |
unsigned long |
Index of the attribute |
Returns: const String& — Attribute name
getAttributeValue¶
| Spice | |
|---|---|
Retrieve the value of the attribute at the given index, in document order. Panics if the node is not an element.
Parameters
| Name | Type | Description |
|---|---|---|
idx |
unsigned long |
Index of the attribute |
Returns: const String& — Attribute value
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
addAttribute¶
| Spice | |
|---|---|
Append an attribute to the element. Panics if the node is not an element. The caller is responsible for avoiding duplicate attribute names.
Parameters
| Name | Type | Description |
|---|---|---|
name |
const String& |
Attribute name |
value |
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
addChild¶
| Spice | |
|---|---|
Append a child node to the element. Panics if the node is not an element. The element takes ownership of the heap-allocated child.
Parameters
| Name | Type | Description |
|---|---|---|
child |
XmlNode* |
Child node to append |
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
Functions¶
deleteXmlNode¶
| Spice | |
|---|---|
Destroys a heap-allocated XML node together with everything it owns and sets the given handle to nil, so releasing a whole tree only takes one call on its root.
Note that this is what sDelete would normally be used for. It cannot be used here, because a node tree is destroyed recursively and the compiler does not emit an explicit dtor that is only reached through sDestruct.
Parameters
| Name | Type | Description |
|---|---|---|
node |
XmlNode*& |
Handle of the node to destroy |
XmlNodeKind enum¶
Tag for the variant stored inside an XmlNode.
| Item | Value | Description |
|---|---|---|
XML_ELEMENT |
||
XML_TEXT |