lua-users home
lua-l archive

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

Hi Eva,

Have you considered using luaexpat - ?

Andrew Wilson

On 7/9/07, Eva Schmidt <> wrote:
Hello everybody,

I'm currently using the simple XML-Parser from R. Ierusalimschy published on It works fine, but it puts every tag and
attribute in an XML-file onto a stack. So the structure gets a little bit large.
I would need a mor flat structure.
I tried to parse the stack and create a more flat Lua table instead. That worked
also, but the approach was very special to my problem (it parses the stack
recursively and collects label-value pairs and creates for any recursion a new
nested table).
The problem with this are the duplicate labels, that should create different
table instead of overwriting the old ones...

Does anybody have simliar problems and maybe some good ideas or approaches how
to convert the deep Lua stack to a flat table, including the dealing with
duplicate labels?

Here's some example code from me, but I'm not really happy with that:

-- parse stack and put instrument data in tables
-- counters
local c = 0
local x = 1
function parse_stack (tbl, result_tbl, tags)

    for j = 1, table.getn(tbl) do
       local value, label

         for i,v in pairs (tbl[j]) do

            if i == "label" then
               label = v
            elseif i == "xarg" then

               if (tbl[j][i].Value) then
                  value = tbl[j][i].Value

            if value == nil and label then
               -- create a separate table for a complete instrument description
               if label == "Instrument" then
                  c = 0
                  label = x
                  result_tbl[label] = {}
                  parse_stack (tbl[j], result_tbl[label],tags)
                  x = x + 1
                  parse_stack (tbl[j], result_tbl, tags)

            elseif value and label then
               build_instrument (label, value, result_tbl, c, tags)
    -- in case of duplicate tags append a number to differentiate
    c = c + 1

-- utility for building an instrument
function build_instrument (label, value, tbl, count, tags)

    -- extend name of duplicate labels
    if (tbl[label]) then
       label = label .. "_" .. count

    table.insert (tags, label)
    tbl[label] = value

Thanks for any idea!

All the best!