Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- -- luatbl2xml
- -- A very simple converter from lua tables to xml
- function luatbl2xml (tbl, tb)
- local tab = tb or 0;
- local eltname = '';
- local attrs = ''
- -- Get the tagname and attributes
- for k, v in pairs (tbl) do
- if type (k) == "number" then
- if k == 1 then
- eltname = v;
- end
- elseif type (k) == "string" then
- attrs = attrs .. ' ' .. k .. '="' .. v .. '"'
- end
- end
- -- Go recursively into children
- local children = ''
- for k, v in pairs (tbl) do
- if k ~= 1 and type(v) == 'table' then
- children = children .. luatbl2xml (v, tab+2) .. '\n';
- end
- end
- if children ~= '' then
- local begin_elt = "<" .. eltname .. attrs .. ">\n";
- for i=1, tab do
- begin_elt = ' ' .. begin_elt
- end
- local end_elt = "</" .. eltname .. ">"
- for i=1, tab do
- end_elt = ' ' .. end_elt
- end
- return begin_elt .. children .. end_elt
- else
- local begin_end_elt = "<" .. eltname .. attrs .. "/>";
- for i=1, tab do
- begin_end_elt = ' ' .. begin_end_elt
- end
- return begin_end_elt
- end
- end
- doc =
- {"ncl", id="myid",
- {"body", id="body",
- {"port", component="m1"},
- {"media", id="m1", src="video.mp4"},
- {"link", id="l1",
- {"bind", role="onEnd", component="m1"},
- {"bind", role="start", component="m1"}
- }
- }
- }
- print (luatbl2xml (doc))
Add Comment
Please, Sign In to add comment