LuaExpat
XML Expat parsing for the Lua programming language

Introduction

The "table" parser is another way of representing XML data in Lua tables.

Characteristics

The model represents each XML element as a Lua table. The characteristics of this format are:

  • The XML nodes are represented as a table.
  • child elements are in the array part of the table; where Text nodes are strings, and Child nodes are sub-tables
  • The Tag name is stored in the array at index 0, so outside the regular array range in Lua
  • Attributes are stored in the hash-part of the table, as key-value pairs

Functions

totable.parse(string|function|table|file[, opts])
Parses the input into the table format and returns it. The input can be;
  • string: the entire XML document as a string
  • function: an iterator that returns the next chunk of the XML document on each call, and returns nil when finished
  • table: an array like table that contains the chunks that combined make up the XML document
  • file: an open file handle from which the XML document will be read line-by-line, using read(). Note: the file will not be closed when done.
The second parameter opts is an options table that supports the following options;
  • separator (string): the namespace separator character to use, setting this will enable namespace aware parsing.
  • threat (table): a threat protection options table. If provided the threat protection parser will be used instead of the regular lxp parser.
Upon parsing errors it will return nil, err, line, col, pos.
totable.clean(t)
Traverses the tree recursively, and drops all whitespace-only Text nodes. Returns the (modified) input table.
totable.torecord()
Traverses the tree recursively, and will update every entry that is a Tag with only 1 Text node as child element, to a key-value entry. Note: Any attributes on the converted element will be lost! If the key already exists (duplicate tag names, or an attribute by that name) then it will not update the entry. Returns the (modified) input table.

Examples

For a string like

s = [[
<person id="123">
	<first>John</first>
	<last>Doe</last>
</abc>
]]

A call like

    tab = lxp.totable.parse (s)

Would result in a table equivalent to

tab = {
	[0] = "person",     -- tag names go into array index 0
	id = "123",         -- attribute names go into the hash part of the table
	[1] = "\n\t"        -- Note that the new-line and tab characters are preserved
	on the table
	[2] = {
		[0] = "first",
		[1] = "John",
	},
	[3] = "\n\t"
	[4] = {
		[0] = "last",
		[1] = "Doe",
	},
	[5] = "\n"
}

After a call to clean like this lxp.totable.clean (tab) the empty whitespace elements will be removed:

tab = {
	[0] = "person",
	id = "123",
	[1] = {
		[0] = "first",
		[1] = "John",
	},
	[3] = {
		[0] = "last",
		[1] = "Doe",
	},
}

After a call to torecord like this lxp.totable.torecord (tab) the single-textfield nodes are turned into key-value pairs:

tab = {
	[0] = "person",
	id = "123",
	first = "John",
	last = "Doe",
}