Commit c6285698 authored by Antoine Cellerier's avatar Antoine Cellerier

Fix simplexml (still has issues with <bla/> tags)

parent 43848e7e
...@@ -54,6 +54,10 @@ end ...@@ -54,6 +54,10 @@ end
-- print a table (recursively) -- print a table (recursively)
function table_print(t,prefix) function table_print(t,prefix)
local prefix = prefix or "" local prefix = prefix or ""
if not t then
print(prefix.."/!\\ nil")
return
end
for a,b in pairs_sorted(t) do for a,b in pairs_sorted(t) do
print(prefix..tostring(a),b) print(prefix..tostring(a),b)
if type(b)==type({}) then if type(b)==type({}) then
......
...@@ -37,25 +37,32 @@ local function parsexml(stream) ...@@ -37,25 +37,32 @@ local function parsexml(stream)
local parents = {} local parents = {}
while reader:read() > 0 do while reader:read() > 0 do
local nodetype = reader:node_type() local nodetype = reader:node_type()
--print(nodetype, reader:name())
if nodetype == 'startelem' then if nodetype == 'startelem' then
local name = reader:name() local name = reader:name()
local node = { name: '', attributes: {}, children: {} } local node = { name= '', attributes= {}, children= {} }
node.name = name node.name = name
while reader:NextAttr() == 0 do while reader:next_attr() == 0 do
node.attributes[reader:Name()] = reader:Value() node.attributes[reader:name()] = reader:value()
end end
if tree then if tree then
tree.children[#tree.children] = node table.insert(tree.children, node)
parents[#parents] = tree table.insert(parents, tree)
tree = node
end end
tree = node
elseif nodetype == 'endelem' then elseif nodetype == 'endelem' then
tree = parents[#parents-1] if #parents > 0 then
tree = parents[#parents]
table.remove(parents)
end
elseif nodetype == 'text' then elseif nodetype == 'text' then
node.children[#node.children] = reader:Value() table.insert(tree.children, reader:value())
end end
end end
if #parents > 0 then
error("XML parser error/Missing closing tags")
end
return tree return tree
end end
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment