标签:
项目的配置文件是XML,项目中用了脚本语言LUA 5.3,找了几个lua解析xml的库,测试都不怎么样,有些是因为LUA的版本比较旧,用得话还得转(虽然工作量不大),最后找到了luaxml ,发现节点检索超方便。
xml = require(‘LuaXml‘) -- load XML data from file "test.xml" into local table xfile local xfile = xml.load("test.xml") print(type(xfile)) -- search for substatement having the tag "scene" local xscene = xfile:find("id","id","5") -- if this substatement is found if xscene ~= nil then -- print it to screen print(xscene) -- print tag, attribute id and first substatement print(xscene.id,xscene.buildLevel) end --xfile:save"t.xml" print("---\nREADY.") for i,node in pairs(xfile:find("root")) do print("tag is ",node.tag) if node.tag~=nil and node[node.tag]=="id" then print(node.id,node.buildLevel,node.upGrade) end end local f2 = xml.load("test2.xml") local section = f2:find("section","id","0") local difficulty=section:find("difficulty","id","1") local level=difficulty:find("level","id","0") print(level.point)
function xml.find(var, tag, attributeKey,attributeValue)
recursively parses a Lua table for a substatement fitting to the provided tag and attribute
http://viremo.eludi.net/LuaXML/index.html#download
标签:
原文地址:http://www.cnblogs.com/loker/p/4378772.html