-
Notifications
You must be signed in to change notification settings - Fork 74
/
example3.lua
executable file
·37 lines (28 loc) · 1.03 KB
/
example3.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
#!/usr/bin/env lua
---Sample application to read a XML file and print it on the terminal.
--This example shows how the module deals with duplicated XML tags,
--either if they have a value or not.
--Use of of the XML below and comment the other ones to see the difference
--@author Manoel Campos da Silva Filho - http://manoelcampos.com
local xml2lua = require("xml2lua")
print("xml2lua v" .. xml2lua._VERSION.."\n")
--Uses a handler that converts the XML to a Lua table
local handler = require("xmlhandler.tree")
local xml = "<tag><tag1/><tag1/></tag>"
--local xml = "<tag><tag1/></tag>"
--local xml = "<tag><tag1>A</tag1><tag1>B</tag1></tag>"
--local xml = "<tag><tag1>A</tag1></tag>"
--Instantiates the XML parser
local parser = xml2lua.parser(handler)
parser:parse(xml)
local data = handler.root.tag
if #data.tag1 > 1 then
data = data.tag1
end
print("\nManual print")
for i, p in pairs(data) do
print(" " .. i, p)
end
print("\nRecursive print")
--Recursivelly prints the table in an easy-to-ready format
xml2lua.printable(handler.root)