summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPixel <pixel@nobis-crew.org>2009-04-21 18:13:41 -0700
committerPixel <pixel@nobis-crew.org>2009-04-21 18:13:41 -0700
commit7adc856d72fbe1fecc399cb462e108e8cc1d101a (patch)
tree9efa953c4cd126bd559e018320fd9c524705616d
parent0230979a0f5df5304f87827da06b76830500015c (diff)
Adding flatten_xml() function.
-rw-r--r--lib/xmllib.lua42
1 files changed, 41 insertions, 1 deletions
diff --git a/lib/xmllib.lua b/lib/xmllib.lua
index 4ca86c7..7bfe36a 100644
--- a/lib/xmllib.lua
+++ b/lib/xmllib.lua
@@ -298,7 +298,7 @@ end
function get_xml_section(t, s, norecurs, matches)
local found, r
- function get_xml_section_r(t, s, norecurs, matches)
+ local function get_xml_section_r(t, s, norecurs, matches)
local k, v, found, r
for k, v in pairs(t) do
if type(v) == "table" then
@@ -341,6 +341,46 @@ function get_xml_section(t, s, norecurs, matches)
return r
end
+-- Very harsh function, but working for some XML cases.
+function flatten_xml(t)
+ local function flatten_xml_r(t)
+ local r, n = {}
+ local i
+
+ if type(t.n) ~= "number" then
+ if type(t.name) == "string" then
+ return r, t.name
+ else
+ error "Wrong XML structure."
+ end
+ end
+
+ if t.n == 1 and type(t[1]) ~= "table" then
+ r = t[1]
+ else
+ for i = 1, t.n do
+ local cr, cn = flatten_xml_r(t[i])
+ if r[cn] then
+ if not r[cn].__complex then
+ local ref = r[cn]
+ r[cn] = { __complex = true }
+ table.insert(r[cn], ref)
+ end
+ table.insert(r[cn], cr)
+ else
+ r[cn] = cr
+ end
+ end
+ end
+
+ return r, t.name
+ end
+
+ local r = flatten_xml_r(t)
+
+ return r
+end
+
--[[ example of use:
result = [=[