-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxml.lua
More file actions
147 lines (140 loc) · 3.68 KB
/
xml.lua
File metadata and controls
147 lines (140 loc) · 3.68 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
module(..., package.seeall)
--
-- Various XML related functions
-- Stuff really mostly only works for a few use cases
--
--
-- Print out a full table
-- Only really works with tables that mostly only have numbers and strings
-- as keys and values (and tables as values)
--
function PrintTable(t,base)
base = base or ''
print(base..' = {}')
for i,v in pairs(t) do
local pi = type(i)=='number' and '['..i..']' or '.'..i
if type(v) == 'table' then
PrintTable(v, base..pi)
else
local pv = type(v)=='string' and '"'..v..'"' or v
print(base..pi..' = '..pv)
end
end
end
--
-- Kinda sorta writes XML
--
function WriteXML(t, base, tag)
local final = ''
if not base then
final = '<?xml version="1.0" encoding="UTF-8"?>\n'
final = final .. '<root>\n'
base = ''
tag =''
end
local xarg = {}
local empty = true
if #tag > 0 then
for i,v in pairs(t) do if type(v) == 'table' then empty = false end end
if not empty then final = final..base..'<'..tag..'>\n' end
end
for i,v in pairs(t) do
if type(v) == 'table' then
final = final..WriteXML(v, base..' ', type(i) == 'number' and tag:sub(1,#tag-1) or i)
else
xarg[i] = v
empty = true
end
end
local xargs = ""
for i,v in pairs(xarg) do xargs = xargs..' '..i..'="'..v..'"' end
if #tag > 0 then
if empty then final = final..base..'<'..tag..xargs..'/>\n'
else final = final..base..'</'..tag..'>\n' end
else
final = final..'</root>\n'
end
return final
end
--
-- XML Parsing Stuff
--
-- LoadXML from http://lua-users.org/wiki/LuaXml
function LoadXML(s)
local function LoadXML_parseargs(s)
local arg = {}
string.gsub(s, "(%w+)=([\"'])(.-)%2", function (w, _, a)
arg[w] = a
end)
return arg
end
local stack = {}
local top = {}
table.insert(stack, top)
local ni,c,label,xarg, empty
local i, j = 1, 1
while true do
ni,j,c,label,xarg, empty = string.find(s, "<(%/?)([%w:]+)(.-)(%/?)>", i)
if not ni then break end
local text = string.sub(s, i, ni-1)
if not string.find(text, "^%s*$") then
table.insert(top, text)
end
if empty == "/" then -- empty element tag
table.insert(top, {label=label, xarg=LoadXML_parseargs(xarg), empty=1})
elseif c == "" then -- start tag
top = {label=label, xarg=LoadXML_parseargs(xarg)}
table.insert(stack, top) -- new level
else -- end tag
local toclose = table.remove(stack) -- remove top
top = stack[#stack]
if #stack < 1 then
error("nothing to close with "..label)
end
if toclose.label ~= label then
error("trying to close "..toclose.label.." with "..label)
end
table.insert(top, toclose)
end
i = j+1
end
local text = string.sub(s, i)
if not string.find(text, "^%s*$") then
table.insert(stack[#stack], text)
end
if #stack > 1 then
error("unclosed "..stack[stack.n].label)
end
return stack[1]
end
--
-- Find the first instance of something from a base node
--
function FindInXML(node, label)
if not node or node.label == label then return node end
for i, v in ipairs(node) do
if type(v) == 'table' then
local fn = FindInXML(v, label)
if type(fn) == 'table' and fn.label == label then return fn end
end
end
return nil
end
--
-- Find all instances of something from a base node
--
function FindAllInXML(node, label)
local ret = {}
if node then
if node.label == label then table.insert(ret, node) end
for i, v in ipairs(node) do
if type(v) == 'table' then
local fn = FindAllInXML(v, label)
if #fn > 0 then
for j, nd in pairs(fn) do table.insert(ret, nd) end
end
end
end
end
return ret
end