I'm trying to parse data from XML files like
<level>
<bg>details1</bg>
<bg>details2</bg>
</level>
With xml.find(bg) I can only get details 1 out. It's because xml.find Returns the first (sub-)table which matches the search condition or nil.
If I want to read both bg out. How could I achieve it in LuaXML? Or please introduce other Lua XML library works.
Addons My real scenario is like this
<a>
<b>
<level>
<bg>details1</bg>
</level>
<level>
<bg>details2</bg>
</level>
</b>
</a>
I know I need to get whole b object out and use xml.tag to read level out. But my attempts fail. Could you help me on this code?
I finally get my solution like this based on Mike Corcoran's suggestion.
require 'luaxml'
local text = [[
<a>
<bcde>
<level>
<bg>details1</bg>
</level>
<level>
<bg>details2</bg>
</level>
</bcde>
</a>
]]
local txml = xml.eval(text)
for _, node in pairs(txml:find("bcde")) do
if node.TAG ~= nil then
if node[node.TAG] == "level" then
local bg = node:find("bg")
if bg ~= nil then
for i=1, #bg do
print( bg[i])
end
end
end
end
end
There are too many layers and seems slow.. Any suggestion to improve efficiency?