Skip to content

Commit 42f9b60

Browse files
committed
Makes the tableName parameter in toXml() function optional.
Signed-off-by: Manoel Campos <[email protected]>
1 parent b32815c commit 42f9b60

File tree

1 file changed

+8
-2
lines changed

1 file changed

+8
-2
lines changed

xml2lua.lua

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -171,15 +171,17 @@ end
171171
---Converts a Lua table to a XML String representation.
172172
--@param tb Table to be converted to XML
173173
--@param tableName Name of the table variable given to this function,
174-
-- to be used as the root tag.
174+
-- to be used as the root tag. If a value is not provided
175+
-- no root tag will be created.
175176
--@param level Only used internally, when the function is called recursively to print indentation
176177
--
177178
--@return a String representing the table content in XML
178179
function xml2lua.toXml(tb, tableName, level)
179180
local level = level or 1
180181
local firstLevel = level
181182
local spaces = string.rep(' ', level*2)
182-
local xmltb = {}
183+
tableName = tableName or ""
184+
local xmltb = (tableName ~= '' and level == 1) and {'<'..tableName..'>'} or {}
183185

184186
for k, v in pairs(tb) do
185187
if type(v) == "table" then
@@ -212,6 +214,10 @@ function xml2lua.toXml(tb, tableName, level)
212214
end
213215
end
214216

217+
if tableName ~= '' and firstLevel == 1 then
218+
table.insert(xmltb, '</'..tableName..'>\n')
219+
end
220+
215221
return table.concat(xmltb, "\n")
216222
end
217223

0 commit comments

Comments
 (0)