luaxml 确实好用,节点检索超方便

本文介绍了一个Lua语言下用于解析XML文件的库——LuaXML。通过示例代码展示了如何加载XML文件到Lua表中,并提供了查找特定节点的方法。此外,还讨论了如何递归地搜索表以找到符合指定标签和属性的子节点。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

转载于:https://www.cnblogs.com/loker/p/4378772.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值