Skip to content

pyio.write_xml

Daniel Flassig edited this page Jul 7, 2026 · 3 revisions

Writes a Lua table as an XML file.

pyio.write_xml(file, element)
Parameter Type Description
file path handle The file to write. The path handle must grant write access.
element table A Lua table describing the XML element tree

Return value

Type Description
boolean true on success, false if the file could not be written (see remarks)

Example:

Writing the following Lua table

local element = {
  local_name = "root",
  prefix = "",
  namespace_uri = "",
  _attributes = {
    version = "1.0"
  },
  {
    local_name = "item",
    prefix = "",
    namespace_uri = "",
    "Hello World"
  }
}

local folder = pyui.select_folder("full")
if folder then
    pyio.write_xml(folder:append_path("sample.xml"), element)
end

will create an XML file with the following content:

<root version="1.0">
  <item>Hello World</item>
</root>

Remarks:

The function writes a Lua table as an XML element tree. It is not restricted to tables returned by pyio.parse_xml().

Each XML element is represented by a Lua table with the following fields:

Property Type Description
local_name string The local name of the element
prefix string The namespace prefix of the element, or an empty string
namespace_uri string The namespace URI of the element, or an empty string
_attributes table Optional table of XML attributes
_namespaces table Optional table of namespace declarations
1, 2, ... string or table Child nodes, either text nodes or nested element tables

Child nodes are written in numeric order. String values are written as text nodes. Nested tables are written as child elements.

If _namespaces is present, its entries are written as namespace declarations (xmlns attributes).

If _attributes is present, its entries are written as XML attributes.

The output is formatted with indentation for readability.

Comments, processing instructions, doctypes, and other non-element node types are not generated by this function.

The output encoding is UTF-8.

A writable path handle can be obtained from pyui.select_folder with "full" access. Passing a read-only handle raises an error. false is returned when the file cannot be opened or written.

Version Support:

Minimum PYTHA Version: V26

See also:

pyio, pyio.parse_xml, pyio.xml_find, pyui.select_folder

Clone this wiki locally