dreamworkAPI
Ctrl K
GitHub
classDocs / XMLNode

XMLNode

classsharedmenusource
Object → XMLNode

A XML node object, representing a node in an XML document.

Can be converted to string using :toString() or tostring().

Usage:

lua
local root = XMLNode.fromString( [[
<root>
    <child>
        <title>Test Title</title>
    </child>
</root>
]] )

print( root:toString( false ) ) -- <root><child><title>Test Title</title></child></root>

Constructor

XMLNode(name: string, attributes: ( table<string, string> | nil ), parent: ( XMLNode | nil )): XMLNode

Fields

NameTypeDescription
namestringThe name of the node.
attributesnil | table<string, (string | nil)>The attributes of the node.
parentXMLNode | nilThe parent node.
datanil | string | table<string, (string | XMLNode | XMLNode.Value[])>The data of the node.
ordernil | (string | integer)[]The document order of the children, element names and indices of texts, maintained automatically.
textsnil | string[]The text chunks in document order, together they are the text slot [1], maintained automatically.

Static functions

fromStringConverts an XML string to an XML node structure.
fromTableConverts a table to an XML node structure.

#XMLNode.fromString

sharedmenunodiscardsource
function XMLNode.fromString(xml_str: string, keep_whitespace?: boolean): XMLNode | nil

Converts an XML string to an XML node structure.

By default the text of text-only elements is trimmed, in mixed content only indentation (whitespace with line breaks) is removed and whitespace-only text is skipped.

With keep_whitespace or xml:space="preserve" all text is kept exactly as written.

Line breaks are normalized to \n, attribute values are normalized as XML requires, character references and entities declared in the internal DOCTYPE subset are decoded.

Parameters
NameTypeDescription
xml_strstringThe XML string to parse.
keep_whitespaceoptionalbooleanWhether to keep all whitespace in text, defaults to false.
Returns
TypeNameDescription
XMLNode | nilroot_nodeThe root node, or nil if the string has no elements.

#XMLNode.fromTable

sharedmenunodiscardsource
function XMLNode.fromTable(tbl: table, name?: string): XMLNode

Converts a table to an XML node structure.

Parameters
NameTypeDescription
tbltableThe table to convert to an XML node.
nameoptionalstringThe name of the root node.
Returns
TypeNameDescription
XMLNoderoot_nodeThe root node of the XML tree.

Methods

childrenReturns an iterator over the children of the node at the given key in document order, supporting dot-separated keys for…
containsChecks if the node contains a value at the given key, supporting dot-separated keys for nested values.
containsListChecks if the node contains a list at the given key, supporting dot-separated keys for nested values.
containsNodeChecks if the node contains a node at the given key, supporting dot-separated keys for nested values.
containsStringChecks if the node contains a string at the given key, supporting dot-separated keys for nested values.
getReturns the value of the node at the given key or nil if not found.
getAttributeReturns the value of the attribute with the given name or nil if not found.
getLengthReturns the length of the list at the given key, supporting dot-separated keys for nested values.
isEmptyChecks whether the node is empty.
iteratorReturns an iterator function for the node at the given key, supporting dot-separated keys for nested values.
popPops a value from the list at the given key, supporting dot-separated keys for nested values.
pushPushes a value to the list at the given key, supporting dot-separated keys for nested values.
removeRemoves the value at the given key, supporting dot-separated keys for nested values.
setSets the value of the node at the given key, supporting dot-separated keys for nested values.
setAttributeSets the value of the attribute with the given name.
toStringReturns the XML string representation of this node.
toTableConverts XML node structure to a Lua table.

#XMLNode:children

sharedmenunodiscardsource
function XMLNode:children(key?: string): fun(): ( ( string | integer | nil ), ( string | XMLNode ) )

Returns an iterator over the children of the node at the given key in document order, supporting dot-separated keys for nested values.

Text is returned with key 1 and elements with their name, values are strings or nodes.

Usage:

lua
local root = XMLNode.fromString( "<p>Hello <b>big</b> world</p>" )

for name, value in root:children() do
    print( name, value ) -- 1 "Hello ", b "big", 1 " world"
end
Parameters
NameTypeDescription
keyoptionalstringThe key of the node to iterate over, with dot-separated keys for nested values.
Returns
TypeNameDescription
fun(): ( ( string | integer | nil ), ( string | XMLNode ) )fnThe iterator function.

#XMLNode:contains

sharedmenunodiscardsource
function XMLNode:contains(key: string): boolean

Checks if the node contains a value at the given key, supporting dot-separated keys for nested values.

Parameters
NameTypeDescription
keystringThe key to check, with dot-separated keys for nested values.
Returns
TypeNameDescription
booleanis_containsWhether the node contains a value at the given key.

#XMLNode:containsList

sharedmenunodiscardsource
function XMLNode:containsList(key: string): boolean

Checks if the node contains a list at the given key, supporting dot-separated keys for nested values.

Parameters
NameTypeDescription
keystringThe key to check, with dot-separated keys for nested values.
Returns
TypeNameDescription
booleanis_containsWhether the node contains a list at the given key.

#XMLNode:containsNode

sharedmenunodiscardsource
function XMLNode:containsNode(key: string): boolean

Checks if the node contains a node at the given key, supporting dot-separated keys for nested values.

Parameters
NameTypeDescription
keystringThe key to check, with dot-separated keys for nested values.
Returns
TypeNameDescription
booleanis_containsWhether the node contains a node at the given key.

#XMLNode:containsString

sharedmenunodiscardsource
function XMLNode:containsString(key: string): boolean

Checks if the node contains a string at the given key, supporting dot-separated keys for nested values.

Parameters
NameTypeDescription
keystringThe key to check, with dot-separated keys for nested values.
Returns
TypeNameDescription
booleanis_containsWhether the node contains a string at the given key.

#XMLNode:get

sharedmenunodiscardsource
function XMLNode:get(key: string): nil | string | XMLNode | XMLNode.Value[]

Returns the value of the node at the given key or nil if not found.

Each key segment can be indexed with [n], e.g. child[2].title, key "1" returns the text next to child nodes and an empty key "" returns the text of a text-only node.

Parameters
NameTypeDescription
keystringThe key to look up in the node's data, with dot-separated keys for nested values.
Returns
TypeNameDescription
nil | string | XMLNode | XMLNode.Value[]valueThe value of the node at the given key, or nil if not found.

#XMLNode:getAttribute

sharedmenunodiscardsource
function XMLNode:getAttribute(name: string): nil | string

Returns the value of the attribute with the given name or nil if not found.

Parameters
NameTypeDescription
namestringThe name of the attribute to get.
Returns
TypeNameDescription
nil | stringvalueThe value of the attribute or nil if not found.

#XMLNode:getLength

sharedmenunodiscardsource
function XMLNode:getLength(key: string): integer

Returns the length of the list at the given key, supporting dot-separated keys for nested values.

Parameters
NameTypeDescription
keystringThe key to get the length of, with dot-separated keys for nested values.
Returns
TypeNameDescription
integerlengthThe length of the list at the given key.

#XMLNode:isEmpty

sharedmenunodiscardsource
function XMLNode:isEmpty(): boolean

Checks whether the node is empty.

Returns
TypeNameDescription
booleanis_empty

#XMLNode:iterator

sharedmenunodiscardsource
function XMLNode:iterator(key?: string): fun(): ( ( string | nil ), ( string | XMLNode ) )

Returns an iterator function for the node at the given key, supporting dot-separated keys for nested values.

Values are returned in map order, use :children() for document order.

Text of a text-only node is returned with an empty key "", text next to child nodes with key 1, list items are returned one by one with the list key.

Parameters
NameTypeDescription
keyoptionalstringThe key to iterate over, with dot-separated keys for nested values.
Returns
TypeNameDescription
fun(): ( ( string | nil ), ( string | XMLNode ) )fnThe iterator function, which returns key-value pairs for the node at the given key.

#XMLNode:pop

sharedmenusource
function XMLNode:pop(key: string): nil | string | XMLNode

Pops a value from the list at the given key, supporting dot-separated keys for nested values.

Parameters
NameTypeDescription
keystringThe key to pop the value from, with dot-separated keys for nested values.
Returns
TypeNameDescription
nil | string | XMLNodeThe popped value, or nil if the key does not exist or the value is not a node.

#XMLNode:push

sharedmenusource
function XMLNode:push(key: string, value: string | XMLNode)

Pushes a value to the list at the given key, supporting dot-separated keys for nested values.

Pushing a string to the text slot "1" appends it to the text.

Parameters
NameTypeDescription
keystringThe key to push the value to, with dot-separated keys for nested values.
valuestring | XMLNodeThe value to push to the list.

#XMLNode:remove

sharedmenusource
function XMLNode:remove(key: string): boolean

Removes the value at the given key, supporting dot-separated keys for nested values.

Parameters
NameTypeDescription
keystringThe key to remove, with dot-separated keys for nested values.
Returns
TypeNameDescription
booleansuccessReturns true if the value was removed, false otherwise (e.g. key not found).

#XMLNode:set

sharedmenusource
function XMLNode:set(key: string, value: XMLNode.Value | XMLNode.Value[] | nil)

Sets the value of the node at the given key, supporting dot-separated keys for nested values.

Missing or non-node intermediate values are replaced with new empty nodes.

Parameters
NameTypeDescription
keystringThe key to set, with dot-separated keys for nested values.
valueXMLNode.Value | XMLNode.Value[] | nilThe value to set, which can be a string or a node, or nil to remove the value.

#XMLNode:setAttribute

sharedmenusource
function XMLNode:setAttribute(name: string, value: string | nil)

Sets the value of the attribute with the given name.

Parameters
NameTypeDescription
namestringThe name of the attribute to set.
valuestring | nilThe value to set for the attribute, or nil to remove the attribute.

#XMLNode:toString

sharedmenunodiscardsource
function XMLNode:toString(pretty?: boolean, name?: string, level?: number): string

Returns the XML string representation of this node.

Children are written in document order, pretty formatting adds whitespace to the text, so use compact output for exact data.

Parameters
NameTypeDescription
prettyoptionalbooleanWhether to use pretty formatting (indentation and newlines).
nameoptionalstringThe name of the node.
leveloptionalnumberThe indentation level.
Returns
TypeNameDescription
stringstrThe XML string representation of this node.

#XMLNode:toTable

sharedmenunodiscardsource
function XMLNode:toTable(): table

Converts XML node structure to a Lua table.

Text content is stored in slot [1], attributes and document order are not included.

Returns
TypeNameDescription
table
Generated from the dreamwork meta branch.