|
| LuaExpat |
| Lua Object Model |
Lua Object Model (LOM) is a representation of XML elements through Lua data types. Currently it is not supposed to be 100% complete, but simple.
LuaExpat's distribution provides an implementation of LOM that gets a XML documenta (a string) and transforms it to a Lua table. The only function exported is lom.parse.
The model represents each XML element as a Lua table. A LOM table has three special characteristics:
tag that holds the element's name;
attr that stores the element's attributes (see attribute's section); and
The special field attr is a Lua table that stores the XML element's attributes as pairs <key>=<value>. To assure an order (if necessary), the sequence of keys could be placed at the array-part of this same table.
For a simple string like
s = [[<abc a1="A1" a2="A2">inside tag `abc'</abc>]]
A call like
tab = lom.parse (s))
Would result in a table equivalent to
tab = {
["attr"] = {
[1] = "a1",
[2] = "a2",
["a2"] = "A2",
["a1"] = "A1",
},
[1] = "inside tag `abc'",
["tag"] = "abc",
}
Now an example with an element inside another one
tab = lom.parse(
[[<qwerty q1="q1" q2="q2">
<asdf>some text</asdf>
</qwerty>]]
)
The results would have been a table equivalent to
tab = {
[1] = "\
",
[2] = {
["attr"] = {
},
[1] = "some text",
["tag"] = "asdf",
},
["attr"] = {
[1] = "q1",
[2] = "q2",
["q2"] = "q2",
["q1"] = "q1",
},
[3] = "\
",
["tag"] = "qwerty",
}
Note that even the new-line and tab characters are stored on the table.