Skip to content

Commit

Permalink
feat: add get_attrs function
Browse files Browse the repository at this point in the history
  • Loading branch information
Crazyokd committed Oct 9, 2024
1 parent b7450d6 commit ffa5e6a
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 62 deletions.
29 changes: 20 additions & 9 deletions luaxml.lua
Original file line number Diff line number Diff line change
Expand Up @@ -304,7 +304,7 @@ function luaxml:parseNormalTag(xml, f)
else
table.insert(self._stack, tag.name)

self:add_attr(tag)
self:add_tag(tag)

-- Self-Closing Tag
if (f.endt2 == "/") then
Expand Down Expand Up @@ -399,7 +399,7 @@ function luaxml:parse(xml, parseAttributes)
f.endText = f.match + string.len(f.text) - 1
f.match = f.match + string.len(f.text)
f.text = parseEntities(stripWS(f.text))
self:add_node(f.text)
self:set_val(f.text)

self:parseTagType(xml, f)
f.pos = f.endMatch + 1
Expand Down Expand Up @@ -468,13 +468,12 @@ local function add_eitem(obj, idx)
end

-- construct inner xml table
function luaxml:add_node(v)
function luaxml:set_val(v)
local path = '/' .. table.concat(self._stack, '/')
if string.len(v) ~= 0 then
-- print("path", path, v)
-- self:construct(path, v)
local val = v
if type(val) == "table" then
if type(v) == "table" then
return
end

Expand All @@ -492,11 +491,11 @@ function luaxml:add_node(v)
n = iter()
end

obj["@val"] = val
obj["@val"] = v
end
end

function luaxml:add_attr(tag)
function luaxml:add_tag(tag)
local path = '/' .. table.concat(self._stack, '/')
-- print("path", path, tag.name, tag.attrs)
-- self:construct(path, nil, tag.attrs)
Expand Down Expand Up @@ -679,8 +678,20 @@ function luaxml:get(path, xt)
end
end

function luaxml:get_attr(obj, attr)
return obj["@attr"][attr]
function luaxml:get_attrs(path)
local obj = self.xt
-- find all node name and iterate it.
for n in string.gmatch(path, "/([^/]+)") do
local s, e = string.find(n, "%[%d+%]", 1, false)
if s then
obj = obj[string.sub(n, 1, s - 1)][tonumber(string.sub(n, s + 1, e - 1))]
print(obj)
else
obj = obj[n]
end
end

return obj["@attr"]
end

-- 元素名称必须以字母或下划线(_)开头
Expand Down
32 changes: 20 additions & 12 deletions test.lua
Original file line number Diff line number Diff line change
@@ -1,24 +1,25 @@
local function print_r(root)
local cache = { [root] = "." }
local cache = { [root] = "." }

local function _dump(t,space,name)
local function _dump(t, space, name)
local temp = {}
for k,v in pairs(t) do
for k, v in pairs(t) do
local key = tostring(k)
if cache[v] then
table.insert(temp,"+" .. key .. " {" .. cache[v].."}")
table.insert(temp, "+" .. key .. " {" .. cache[v] .. "}")
elseif type(v) == "table" then
local new_key = name .. "." .. key
cache[v] = new_key
table.insert(temp,"+" .. key .. _dump(v,space .. (next(t,k) and "|" or " " ).. string.rep(" ",#key),new_key))
table.insert(temp,
"+" .. key .. _dump(v, space .. (next(t, k) and "|" or " ") .. string.rep(" ", #key), new_key))
else
table.insert(temp,"+" .. key .. " [" .. tostring(v).."]")
table.insert(temp, "+" .. key .. " [" .. tostring(v) .. "]")
end
end
return table.concat(temp,"\n"..space)
return table.concat(temp, "\n" .. space)
end

print(_dump(root, "" , ""))
print(_dump(root, "", ""))
end


Expand All @@ -28,15 +29,13 @@ local lx1 = luaxml.new()
lx1.xt = {
["@meta"] = {},
root = {
-- _val|_key|_idx互斥
-- _attr与_idx互斥
key1 = {
["@val"] = "value1",
["@next"] = "key2",
},
key2 = {
["@val"] = 123,
["@attr"] = {type = "string"},
["@attr"] = { type = "string" },
["@next"] = "key3",
},
key3 = {
Expand All @@ -56,7 +55,8 @@ lx1.xt = {
key42 = {
["@val"] = 123,
},
["@head"] = "key41"
["@head"] = "key41",
["@attr"] = { type = "map" },
},
["@head"] = "key1",
},
Expand All @@ -70,10 +70,18 @@ print(lx1:get("/root/key1"))
lx1:set("/root/key1", 456)
print(lx1:get("/root/key1"))


print(lx1:get("/root/key3[1]"))
lx1:set("/root/key3[1]", 789)
print(lx1:get("/root/key3[1]"))
print(lx1:get("/root/key5"))

lx1:set("/root/key3[3]", 1024)

-- iterate attrs
local key4attrs = lx1:get_attrs("/root/key4")
for k, v in pairs(key4attrs) do
print(k, v)
end

lx1:print()
50 changes: 9 additions & 41 deletions test2.lua
Original file line number Diff line number Diff line change
@@ -1,64 +1,32 @@
local function print_r(root)
local cache = { [root] = "." }
local cache = { [root] = "." }

local function _dump(t,space,name)
local function _dump(t, space, name)
local temp = {}
for k,v in pairs(t) do
for k, v in pairs(t) do
local key = tostring(k)
if cache[v] then
table.insert(temp,"+" .. key .. " {" .. cache[v].."}")
table.insert(temp, "+" .. key .. " {" .. cache[v] .. "}")
elseif type(v) == "table" then
local new_key = name .. "." .. key
cache[v] = new_key
table.insert(temp,"+" .. key .. _dump(v,space .. (next(t,k) and "|" or " " ).. string.rep(" ",#key),new_key))
table.insert(temp,
"+" .. key .. _dump(v, space .. (next(t, k) and "|" or " ") .. string.rep(" ", #key), new_key))
else
table.insert(temp,"+" .. key .. " [" .. tostring(v).."]")
table.insert(temp, "+" .. key .. " [" .. tostring(v) .. "]")
end
end
return table.concat(temp,"\n"..space)
return table.concat(temp, "\n" .. space)
end

print(_dump(root, "" , ""))
print(_dump(root, "", ""))
end

-- local xp = require "xmlparser"
--
-- local xp1 = xp.new({
-- text = function (s, text, nmatch, etext)
-- -- print("text", string.len(text), text, etext)
-- -- print("text")
-- end,
-- -- handle xml declaration
-- decl = function (s, tag, smatch, eMatch)
-- print("decl", tag.name, smatch, eMatch)
-- end,
-- pi = function (s, tag, match, ematch)
-- print("pi", tag, match, ematch)
-- end,
-- comment = function (s, text, next, match, ematch)
-- print("comment", text, next, match, ematch)
-- end,
-- dtd = function (s, tag, match, ematch)
-- print("dtd", tag, match, ematch)
-- end,
-- cdata = function (s, text, match, ematch)
-- print("cdata", text, match, ematch)
-- end,
-- starttag = function (s, tag, match, ematch)
-- print("starttag", tag.name, match, ematch)
-- end,
-- endtag = function (s, tag, match, ematch)
-- print("endtag", tag.name, match, ematch)
-- end,
-- }, {})

local f = assert(io.open("test.xml", "r"))
local xml = f:read("a")
f:close()
-- xp1:parse(xml, nil)

local lxml = require "luaxml"

local lx1 = lxml.new()

lx1:load(xml)
Expand Down

0 comments on commit ffa5e6a

Please sign in to comment.