luaxml 確實好用,節點檢索超方便


  項目的配置文件是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

    • param var, the table to be searched in.
    • param tag (optional) the xml tag to be found.
    • param attributeKey (optional) the exact attribute to be found.
    • param attributeValue (optional) the attribute value to be found.
    • Returns the first (sub-)table which matches the search condition or nil.

 有個坑,

xml = require('LuaXml')這里一定要設置為全局的,加個local會出事的


http://viremo.eludi.net/LuaXML/index.html#download


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM