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* (heap on the root).
Parsing lives in "std/text/xml-parser" and serialization in "std/text/xml-serializer", so programs only pull in what they use.
Constructors¶
ctor¶
| Spice | |
|---|---|
Construct an empty XML element node
ctor¶
| Spice | |
|---|---|
Construct an empty XML node of the given kind
Parameters
| Name | Type | Description |
|---|---|---|
kind |
XmlNodeKind |
Kind of 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
XmlNodeKind enum¶
Tag for the variant stored inside an XmlNode.
| Item | Value | Description |
|---|---|---|
XML_ELEMENT |
||
XML_TEXT |