Repository navigation
Expand file tree
/
Copy pathxmlparser.lua
More file actions
64 lines (56 loc) · 1.49 KB
/
Copy pathxmlparser.lua
File metadata and controls
64 lines (56 loc) · 1.49 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
local xml2lua = require "xml2lua"
local domHandler = require "xmlhandler.dom"
local treeHandler = require "xmlhandler.tree"
local function text2dom(text)
local hand = domHandler:new()
local parser = xml2lua.parser(hand)
parser:parse(text)
return hand.root
end
local function text2table(text)
local hand = treeHandler:new()
local parser = xml2lua.parser(hand)
parser:parse(text)
return hand.root
end
local function dom2table(dom)
if dom._type == "TEXT" then
return {text=dom._text}
end
local t = {}
if #dom._children == 1 then
t[dom._name] = dom2table(dom._children[1])
else
t[dom._name] = {}
for i, child in ipairs(dom._children) do
if child._type == "ELEMENT" then
t[dom._name][child._name] = dom2table(child)[child._name]
t[dom._name][child._name]._attr = child._attr
end
end
end
return t
end
local function stripTags(text)
local hand = {
text = function (self, txt)
self.txt = self.txt .. txt
end,
endtag = function() end,
txt = "",
}
local parser = xml2lua.parser(hand)
parser.options.stripWS = nil
parser:parse(text)
return hand.txt
end
local function wrapText(text, tagName, attrs)
attrText = ""
if attrs then
for k,v in pairs(attrs) do
attrText = attrText.." "..k.."="..'"'..tostring(v)..'"'
end
end
return "<"..tagName..attrText..">"..text.."</"..tagName..">"
end
return {text2dom=text2dom, text2table=text2table, stripTags=stripTags, wrapText=wrapText, dom2table=dom2table}