3 -- symbolic name for tag index, this allows accessing the tag by var[xml.TAG]
6 -- sets or returns tag of a LuaXML object
7 function xml.tag(var,tag)
8 if base.type(var)~="table" then return end
9 if base.type(tag)=="nil" then
15 -- creates a new LuaXML object either by setting the metatable of an existing Lua table or by setting its tag
17 if base.type(arg)=="table" then
18 base.setmetatable(arg,{__index=xml, __tostring=xml.str})
22 base.setmetatable(var,{__index=xml, __tostring=xml.str})
23 if base.type(arg)=="string" then var[xml.TAG]=arg end
27 -- appends a new subordinate LuaXML object to an existing one, optionally sets tag
28 function xml.append(var,tag)
29 if base.type(var)~="table" then return end
30 local newVar = xml.new(tag)
35 -- converts any Lua var into an XML string
36 function xml.str(var,indent,tagValue)
37 if base.type(var)=="nil" then return end
38 local indent = indent or 0
40 for i = 1,indent do indentStr=indentStr.." " end
43 if base.type(var)=="table" then
44 local tag = var[0] or tagValue or base.type(var)
45 local s = indentStr.."<"..tag
46 for k,v in base.pairs(var) do -- attributes
47 if base.type(k)=="string" then
48 if base.type(v)=="table" and k~="_M" then -- otherwise recursiveness imminent
49 tableStr = tableStr..xml.str(v,indent+1,k)
51 s = s.." "..k.."=\""..xml.encode(base.tostring(v)).."\""
55 if #var==0 and #tableStr==0 then
57 elseif #var==1 and base.type(var[1])~="table" and #tableStr==0 then -- single element
58 s = s..">"..xml.encode(base.tostring(var[1])).."</"..tag..">\n"
61 for k,v in base.ipairs(var) do -- elements
62 if base.type(v)=="string" then
63 s = s..indentStr.." "..xml.encode(v).." \n"
65 s = s..xml.str(v,indent+1)
68 s=s..tableStr..indentStr.."</"..tag..">\n"
72 local tag = base.type(var)
73 return indentStr.."<"..tag.."> "..xml.encode(base.tostring(var)).." </"..tag..">\n"
78 -- saves a Lua var as xml file
79 function xml.save(var,filename)
80 if not var then return end
81 if not filename or #filename==0 then return end
82 local file = base.io.open(filename,"w")
83 file:write("<?xml version=\"1.0\"?>\n<!-- file \"",filename, "\", generated by LuaXML -->\n\n")
84 file:write(xml.str(var))
89 -- recursively parses a Lua table for a substatement fitting to the provided tag and attribute
90 function xml.find(var, tag, attributeKey,attributeValue)
92 if base.type(var)~="table" then return end
93 if base.type(tag)=="string" and #tag==0 then tag=nil end
94 if base.type(attributeKey)~="string" or #attributeKey==0 then attributeKey=nil end
95 if base.type(attributeValue)=="string" and #attributeValue==0 then attributeValue=nil end
96 -- compare this table:
98 if var[0]==tag and ( attributeValue == nil or var[attributeKey]==attributeValue ) then
99 base.setmetatable(var,{__index=xml, __tostring=xml.str})
103 if attributeValue == nil or var[attributeKey]==attributeValue then
104 base.setmetatable(var,{__index=xml, __tostring=xml.str})
108 -- recursively parse subtags:
109 for k,v in base.ipairs(var) do
110 if base.type(v)=="table" then
111 local ret = xml.find(v, tag, attributeKey,attributeValue)
112 if ret ~= nil then return ret end