4949--
5050-- @author Paul Chakravarti ([email protected] )5151-- @author Manoel Campos da Silva Filho
52- local xml2lua = {_VERSION = " 1.5-1 " }
52+ local xml2lua = {_VERSION = " 1.5-2 " }
5353local XmlParser = require (" XmlParser" )
5454
5555--- Recursivelly prints a table in an easy-to-ready format
@@ -168,6 +168,32 @@ local function getFirstKey(tb)
168168 return tb
169169end
170170
171+ --- Parses a given entry in a lua table
172+ -- and inserts it as a XML string into a destination table.
173+ -- Entries in such a destination table will be concatenated to generated
174+ -- the final XML string from the origin table.
175+ -- @param xmltb the destination table where the XML string from the parsed key will be inserted
176+ -- @param tagName the name of the table field that will be used as XML tag name
177+ -- @param fieldValue a field from the lua table to be recursively parsed to XML or a primitive value that will be enclosed in a tag name
178+ -- @param level a int value used to include indentation in the generated XML from the table key
179+ local function parseTableKeyToXml (xmltb , tagName , fieldValue , level )
180+ local spaces = string.rep (' ' , level * 2 )
181+
182+ local strValue , attrsStr = " " , " "
183+ if type (fieldValue ) == " table" then
184+ attrsStr = attrToXml (fieldValue ._attr )
185+ fieldValue ._attr = nil
186+ -- If after removing the _attr field there is just one element inside it,
187+ -- the tag was enclosing a single primitive value instead of other inner tags.
188+ strValue = # fieldValue == 1 and spaces .. tostring (fieldValue [1 ]) or xml2lua .toXml (fieldValue , tagName , level + 1 )
189+ strValue = ' \n ' .. strValue .. ' \n ' .. spaces
190+ else
191+ strValue = tostring (fieldValue )
192+ end
193+
194+ table.insert (xmltb , spaces .. ' <' .. tagName .. attrsStr .. ' >' .. strValue .. ' </' .. tagName .. ' >' )
195+ end
196+
171197--- Converts a Lua table to a XML String representation.
172198-- @param tb Table to be converted to XML
173199-- @param tableName Name of the table variable given to this function,
179205function xml2lua .toXml (tb , tableName , level )
180206 level = level or 1
181207 local firstLevel = level
182- local spaces = string.rep (' ' , level * 2 )
183208 tableName = tableName or ' '
184209 local xmltb = (tableName ~= ' ' and level == 1 ) and {' <' .. tableName .. ' >' } or {}
185210
@@ -189,25 +214,17 @@ function xml2lua.toXml(tb, tableName, level)
189214 -- In this case, the name of the array is used as tag name for each element.
190215 -- So, we are parsing an array of objects, not an array of primitives.
191216 if type (k ) == ' number' then
192- local attrs = attrToXml (v ._attr )
193- v ._attr = nil
194- table.insert (xmltb ,
195- spaces .. ' <' .. tableName .. attrs .. ' >\n ' .. xml2lua .toXml (v , tableName , level + 1 )..
196- ' \n ' .. spaces .. ' </' .. tableName .. ' >' )
197- else
217+ parseTableKeyToXml (xmltb , tableName , v , level )
218+ else
198219 level = level + 1
199220 -- If the type of the first key of the value inside the table
200221 -- is a number, it means we have a HashTable-like structure,
201222 -- in this case with keys as strings and values as arrays.
202223 if type (getFirstKey (v )) == ' number' then
203- table.insert (xmltb , xml2lua . toXml ( v , k , level ) )
224+ parseTableKeyToXml (xmltb , k , v , level )
204225 else
205226 -- Otherwise, the "HashTable" values are objects
206- local attrs = attrToXml (v ._attr )
207- v ._attr = nil
208- table.insert (xmltb ,
209- spaces .. ' <' .. k .. attrs .. ' >\n ' .. xml2lua .toXml (v , k , level + 1 )..
210- ' \n ' .. spaces .. ' </' .. k .. ' >' )
227+ parseTableKeyToXml (xmltb , k , v , level )
211228 end
212229 end
213230 else
@@ -217,7 +234,7 @@ function xml2lua.toXml(tb, tableName, level)
217234 if type (k ) == ' number' then
218235 k = tableName
219236 end
220- table.insert (xmltb , spaces .. ' < ' .. k .. ' > ' .. tostring ( v ) .. ' </ ' .. k .. ' > ' )
237+ parseTableKeyToXml (xmltb , k , v , level )
221238 end
222239 end
223240
0 commit comments