A lightweight handle to a single node in an XML DOM tree.
Nodes remain valid as long as their owning XmlDocument is alive.
Reading .name, .type, or .value uses __index and does not
require a method call.
@eryx/encoding/xml ModuleXML module – DOM parsing, manipulation, serialisation and XPath queries.
Backed by pugixml. Provides two core types: XmlDocument: An XML document that owns the DOM tree. XmlNode: A lightweight handle to a single element within a document.
local xml = require("xml")
local doc = xml.parse('<root><item id="1">Hello</item></root>')
local root = doc:root()
print(root.name) --> "root"
print(root:child("item"):text()) --> "Hello"
print(root:child("item"):attr("id")) --> "1"
XPath
for _, node in doc:xpath("//item[@id]") do
print(node.name, node:attr("id"))
end
A lightweight handle to a single node in an XML DOM tree.
Nodes remain valid as long as their owning XmlDocument is alive.
Reading .name, .type, or .value uses __index and does not
require a method call.
The element/PI tag name, or "" for text/comment nodes.
Shorthand for the first PCDATA/CDATA child's content (like Text()).
Returns the concatenated text content of this element (first PCDATA/CDATA child).
Sets the text content of this element (creates a PCDATA child if needed).
Returns a list of element children. If name is given, only children with that tag name are included.
Returns the first child element with the given tag name, or nil if not found.
Appends a new child element with the given tag name.
Prepends a new child element with the given tag name.
Removes a child by tag name or by XmlNode reference.
Returns the next sibling element. If name is given, skips siblings until one with a matching tag name is found.
Returns the previous sibling element.
Returns the value of the attribute name, or nil.
Sets (or creates) the attribute name to value.
Removes the attribute with the given name.
Returns all attributes as a { [name] = value } dictionary.
Evaluates an XPath expression relative to this node and returns all matching nodes.
Evaluates an XPath expression and returns the first match,
or nil. ]]
Evaluates an XPath expression that returns a scalar value (string, number, or boolean) rather than a node set.
Returns the absolute path of this node from the document root.
Uses delimiter (default "/") to separate path segments.
Changes this element's tag name.
An XML document that owns a DOM tree.
The document is the entry point for parsing and serialisation. It exposes XPath queries directly so you don't always need to grab the root element first.
Returns the document element (the single top-level element).
Serialises the entire document to a string.
Indentation string (default "\t").
"raw" | "no_declaration" | nil (default includes a declaration and pretty-prints).
Writes the document to a file on disk.
Evaluates an XPath expression on the whole document and returns all matching nodes.
Evaluates an XPath expression and returns the first match, or nil.
Appends a top-level child element (useful when building documents from scratch with xml.document()).
Creates a new, empty XML document.
Use AppendChild() to add a root element and build the tree programmatically.
Node type string