Skip to content

Commit 957a21a

Browse files
committed
Fix #45
Signed-off-by: Manoel Campos <[email protected]>
1 parent 1206f10 commit 957a21a

File tree

1 file changed

+6
-4
lines changed

1 file changed

+6
-4
lines changed

xml2lua.lua

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ function xml2lua.toXml(tb, tableName, level)
179179
local level = level or 1
180180
local firstLevel = level
181181
local spaces = string.rep(' ', level*2)
182-
local xmltb = level == 1 and {'<'..tableName..'>'} or {}
182+
local xmltb = {}
183183

184184
for k, v in pairs(tb) do
185185
if type(v) == "table" then
@@ -203,13 +203,15 @@ function xml2lua.toXml(tb, tableName, level)
203203
end
204204
end
205205
else
206+
-- If the type of the key is number, the value is an element from an array.
207+
-- In this case, uses the array name (the name of the array) as the tag name.
208+
if type(k) == "number" then
209+
k = tableName
210+
end
206211
table.insert(xmltb, spaces..'<'..k..'>'..tostring(v)..'</'..k..'>')
207212
end
208213
end
209214

210-
if firstLevel == 1 then
211-
table.insert(xmltb, '</'..tableName..'>\n')
212-
end
213215
return table.concat(xmltb, "\n")
214216
end
215217

0 commit comments

Comments
 (0)