Skip to content

Xml Node

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

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
public p XmlNode.ctor()

Construct an empty XML element node

ctor

Spice
public p XmlNode.ctor(XmlNodeKind kind)

Construct an empty XML node of the given kind

Parameters

Name Type Description
kind XmlNodeKind Kind of the 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

setName

Spice
public p XmlNode.setName(const String& name)

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
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

setText

Spice
public p XmlNode.setText(const String& text)

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
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

getAttributeKey

Spice
public f<const String&> XmlNode.getAttributeKey(unsigned long idx)

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
public f<const String&> XmlNode.getAttributeValue(unsigned long idx)

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
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

addAttribute

Spice
public p XmlNode.addAttribute(const String& name, const String& value)

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
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

addChild

Spice
public p XmlNode.addChild(XmlNode* child)

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
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

XmlNodeKind enum

Tag for the variant stored inside an XmlNode.

Item Value Description
XML_ELEMENT
XML_TEXT