doc:tostring

March 26, 2020

Name

doc:tostring — Output a DOM document as a string

Synopsis

require('xml');

doc:tostring();

Description

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

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

This function outputs the entire document as shown in the following:

<?xml version="1.0" encoding="utf8"?>
<doc>
  <item>I am a child node.</item>
</doc>

Note

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

See Also

node:doc and xml.parsexml