Skip to content

pyio.write_xml

fabian-flassig edited this page Mar 18, 2026 · 3 revisions

Writes a Lua table as an XML file.

pyio.write_xml(path, element)
Parameter Type Description
path string The file path where the XML should be written
element table A Lua table describing the XML element tree

Return value

Type Description
nil No return value

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"
  }
}

pyio.write_xml("sample.xml", element)

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.

Version Support:

Minimum PYTHA Version: V26

See also:

pyio, pyio.parse_xml, pyio.xml_find

Clone this wiki locally