node:tostring

March 26, 2020

Name

node:tostring — Output a node as a string

Synopsis

require('xml');

node:tostring();

Description

Where node is an XML element, this method returns a string representation of the XML source of the node and its children. This same method is also available as a __tostring metamethod, so node:tostring() and tostring(node) are equivalent.

local doc = xml.parsexml([[<doc></doc>]]);
local node = doc:root();
local child = node:addchild("item");
child:contents("I am a child node.");
print(child:tostring());

In this case the output is as follows:

<doc>
 <item>I am a child node.</item>
</doc>

Note

The print function will automatically convert a node to string so the use of child:tostring in “node:tostring example” is not strictly necessary.

See Also

xml.parsexml