-
Notifications
You must be signed in to change notification settings - Fork 31
/
xml.lua
53 lines (47 loc) · 1.27 KB
/
xml.lua
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
-- Beginning of XML output.
-- Unlikely for input data to be valid in XML though.
-- Example:
-- <root count="1" sep="">
-- <usr/local/go count="7" sep="/">
-- <test/fixedbugs/bug028.go count="1" sep="/"/>
-- <test2 count="4" sep="/">
-- <fixedbugs1 count="2" sep="/">
-- <bug205.go count="1" sep="/"/>
-- <bug206.go count="1" sep="/"/>
function tablelength(T)
local count = 0
for _ in pairs(T) do count = count + 1 end
return count
end
function indent(n)
for i=1, n do
io.write(" ")
end
end
function xml(node)
indent(node.depth)
io.write("<")
io.write(node.text)
io.write(" count=\""..node.numMatched.."\"")
io.write(" sep=\""..node.sep.."\"")
if tablelength(node.children) == 0 then
io.write("/>\n")
return
end
io.write(">\n")
local tkeys = {}
-- populate the table that holds the keys
for k in pairs(node.children) do table.insert(tkeys, k) end
-- sort the keys
table.sort(tkeys)
-- use the keys to retrieve the values in the sorted order
for _, k in ipairs(tkeys) do
xml(node.children[k])
end
indent(node.depth)
io.write("</")
io.write(node.text)
io.write(">\n")
end
frangipanni.text = "root"
xml(frangipanni)