lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]


On 2016-08-21 16:45, Marc Balmer wrote:
> I am working on code to easily create XML trees from Lua tables.  What would,
> in your opinion, be a comfortable Lua table notation of XML trees, expressed
> as Lua tables?
> 

In Prosody we have a library called util.stanza which deals with
building and traversing XML trees.  They have the following basic structure:

{ name = 'x', attr = { foo = 'bar' }, tags = { ... }, ... }

Where name and attributes contain the element name and attributes, and
element children are in [1..n].  The 'tags' field contains the subset of
children which are tags.  For serializing to Lua tables, the 'tags'
entry is removed.

The library also provides a bunch of methods for extracting data,
iterating and building XML trees.  I think this is the main feature.

I have also experimented with { name, { attrs }, childs ... }, which can
be more comfortably serialized to JSON-like formats.

{ [0] = name, attrkey = attrvalue, [1..n] = children } could be
considered more Lua-ish I suppose.  Having attributes at the top of the
structure does make it weird to use metatables with methods however, if
you wanted to do that.

-- 
Kim "Zash" Alvefur

Attachment: signature.asc
Description: OpenPGP digital signature