xml parsing - LuaXML parse the XML with multiple tags with the same name -
i'm trying parse data xml files like
<level> <bg>details1</bg> <bg>details2</bg> </level>
with xml.find(bg) can details 1 out. it's because xml.find returns first (sub-)table matches search condition or nil.
if want read both bg out. how achieve in luaxml? or please introduce other lua xml library works.
addons real scenario this
<a> <b> <level> <bg>details1</bg> </level> <level> <bg>details2</bg> </level> </b> </a>
i know need whole b object out , use xml.tag read level out. attempts fail. me on code?
i solution 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) _, node in pairs(txml:find("bcde")) if node.tag ~= nil if node[node.tag] == "level" local bg = node:find("bg") if bg ~= nil i=1, #bg print( bg[i]) end end end end end
there many layers , seems slow.. suggestion improve efficiency?
just iterate on children of level tag (unless there other information in there aren't telling needs filtered)
require 'luaxml' local text = [[ <level> <bg>details1</bg> <bg>details2</bg> </level> ]] local value = 1 local txml = xml.eval(text) _, node in pairs(txml:find("level")) if node.tag ~= nil print(node[value]) end end
and if need filter out except <bg>
tags, can modify loop this:
for _, node in pairs(txml:find("level")) if node.tag ~= nil if node[node.tag] == "bg" print(node[value]) end end end
Comments
Post a Comment