Skip to content

Commit fa711dc

Browse files
committed
Closes #25
Adds xml2lua.toXml() function to convert a table to an XML representation
1 parent b8cc148 commit fa711dc

File tree

5 files changed

+98
-5
lines changed

5 files changed

+98
-5
lines changed

example1.lua

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,4 +33,4 @@ for i, p in pairs(people) do
3333
end
3434

3535
--Recursivelly prints the table in an easy-to-ready format
36-
--xml2lua.printable(handler.root)
36+
--xml2lua.printable(handler.root)

example3.lua

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,10 @@ local xml2lua = require("xml2lua")
1010
--Uses a handler that converts the XML to a Lua table
1111
local handler = require("xmlhandler.tree")
1212

13+
local xml = "<tag><tag1/><tag1/></tag>"
1314
--local xml = "<tag><tag1/></tag>"
14-
--local xml = "<tag><tag1/><tag1/></tag>"
1515
--local xml = "<tag><tag1>A</tag1><tag1>B</tag1></tag>"
16-
local xml = "<tag><tag1>A</tag1></tag>"
16+
--local xml = "<tag><tag1>A</tag1></tag>"
1717

1818
--Instantiates the XML parser
1919
local parser = xml2lua.parser(handler)

example4-table2xml.lua

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
#!/usr/bin/env lua
2+
3+
---Sample application showing how to convert a Lua Table to an XML representation
4+
--@author Manoel Campos da Silva Filho - http://manoelcampos.com
5+
6+
local xml2lua = require("xml2lua")
7+
8+
local people = {
9+
person = {
10+
{_attr={type="natural"}, name="Manoel", city="Palmas-TO"},
11+
{_attr={type="natural"}, name="Breno", city="Palmas-TO"},
12+
{_attr={type="legal"}, name="University of Brasília", city="Brasília-DF"}
13+
}
14+
}
15+
16+
print("People Table\n")
17+
xml2lua.printable(people)
18+
print("\n------------------------------------------------------\n")
19+
20+
print("XML Representation\n")
21+
print(xml2lua.toXml(people, "people"))

xml2lua-1.2-4.rockspec renamed to xml2lua-1.3-0.rockspec

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
package = "xml2lua"
2-
version = "1.2-4"
2+
version = "1.3-0"
33
source = {
44
url = "git://github.com/manoelcampos/xml2lua",
5-
tag = "v1.2-4"
5+
tag = "v1.3-0"
66
}
77
description = {
88
summary = "An XML Parser written entirely in Lua that works for Lua 5.1 to 5.3",

xml2lua.lua

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,4 +138,76 @@ function xml2lua.loadFile(xmlFilePath)
138138
error(e)
139139
end
140140

141+
---Gets an _attr element from a table that represents the attributes of an XML tag,
142+
--and generates a XML String representing the attibutes to be inserted
143+
--into the openning tag of the XML
144+
--
145+
--@param attrTable table from where the _attr field will be got
146+
--@return a XML String representation of the tag attributes
147+
local function attrToXml(attrTable)
148+
local s = ""
149+
local attrTable = attrTable or {}
150+
151+
for k, v in pairs(attrTable) do
152+
s = s .. " " .. k .. "=" .. '"' .. v .. '"'
153+
end
154+
return s
155+
end
156+
157+
---Gets the first key of a given table
158+
local function getFirstKey(tb)
159+
if type(tb) == "table" then
160+
for k, v in pairs(tb) do
161+
return k
162+
end
163+
return nil
164+
end
165+
166+
return tb
167+
end
168+
169+
---Converts a Lua table to a XML String representation.
170+
--@param tb Table to be converted to XML
171+
--@param tableName Name of the table variable given to this function,
172+
-- to be used as the root tag.
173+
--@param level Only used internally, when the function is called recursively to print indentation
174+
--
175+
--@return a String representing the table content in XML
176+
function xml2lua.toXml(tb, tableName, level)
177+
local level = level or 0
178+
local firstLevel = level
179+
local spaces = string.rep(' ', level*2)
180+
local xmltb = level == 0 and {'<'..tableName..'>'} or {}
181+
182+
for k, v in pairs(tb) do
183+
if type(v) == "table" then
184+
--If the keys of the table are a number, it represents an array
185+
if type(k) == "number" then
186+
local attrs = attrToXml(v._attr)
187+
v._attr = nil
188+
table.insert(xmltb,
189+
spaces..'<'..tableName..attrs..'>\n'..xml2lua.toXml(v, tableName, level+1)..
190+
'\n'..spaces..'</'..tableName..'>')
191+
else
192+
level = level + 1
193+
if type(getFirstKey(v)) == "number" then
194+
table.insert(xmltb, spaces..xml2lua.toXml(v, k, level))
195+
else
196+
table.insert(
197+
xmltb,
198+
spaces..'<'..k..'>\n'.. xml2lua.toXml(v, level+1)..
199+
'\n'..spaces..'</'..k..'>')
200+
end
201+
end
202+
else
203+
table.insert(xmltb, spaces..'<'..k..'>'..tostring(v)..'</'..k..'>')
204+
end
205+
end
206+
207+
if firstLevel == 0 then
208+
table.insert(xmltb, '</'..tableName..'>\n')
209+
end
210+
return table.concat(xmltb, "\n")
211+
end
212+
141213
return xml2lua

0 commit comments

Comments
 (0)