How to parse XML documents?
LuaExpat is the best known of the Lua XML bindings. This implements a SAX API where one registers callbacks to handle each element parsed.
The lxp.lom
module brings an XML document into memory as a LOM table (Lua Object Model):
Given this XML fragment:
<abc a1="A1" a2="A2">inside tag `abc'</abc>
lxp.lom.parse
will give you the following table structure:
{
[1] = "inside tag `abc'",
["attr"] = {
[1] = "a1",
[2] = "a2",
["a1"] = "A1",
["a2"] = "A2",
},
["tag"] = "abc",
}
The rules of LOM are simple:
- each element is a table
- The field
tag
is the element tag - The field
attr
is a table of attributes; the array part has the attributes in order, and the map part has the mappings between attributes and values - Any child nodes are in the array part of the element table.