luaXml库的使用方法

来源:互联网 发布:js获取title属性 编辑:程序博客网 时间:2024/05/21 10:14

提供一个xml文件

<?xml version="1.0" encoding="GB2312"?><!-- edited with XMLSpy v2008 sp1 (http://www.altova.com) by shaoyz --><!--[0]--><computer><!--[1];取value的值为file[1]["value"];取name的值为file[1]["thinkpad"]--><id value="1" name="thinkpad"><!--取size的值为file[1][1][1]--><size>17</size><!--去height的值为file[1][2][1]--><height>250</height><!--取该工厂的name的值为file[1][3]["name"]--><factory name="xxxx"/></id><!--[2];取该value的值为file[2]["value"];取name的值为file[2]["name"]--><id value="2" name="thinkpad"><size>12</size><height>150</height></id><id value="3" name="thinkpad"><size>14</size><height>200</height></id></computer>

problem:

1.如何取到父节点的名称?例如,在上述的xml文件中,怎么判断父节点是否为“computer”?

--filePath为上述xml文件的路径local xfile = xml.load(filePath)--xfile[0]对应的就是“computer”。
2. 如何取到子节点以及子节点的属性值?

以上述的xml文件为例,取第一个子节点的下的数据

xfile[1]对应的是value = 1的id的所有内容,因此

xfile[1][0]=="id"

xfile[1]["value"]==1

xfile[1]["name"]==thinkpad

xfile[1][1][0]=="size"

xfile[1][1][1]==14

xfile[1][2][0]=="height"

xfile[1][2][1]=="200"

对于第二个id节点的值,只需要将xfile[1]换成xfile[2]即可。

提供解析该xml的lua脚本

----------------------------------------------------------------------记录下luaXML的使用方法----------------------------------------------------------------------定义lua脚本寻找的路径,因为我的windows下的lua安装路径是:d:\\Program Files\\Lua\\5.1\\lua\\?.lua,另外的processTest脚本的路径为e:\\LUA_PROJECT\\tr\\src\\?.lua。package.path='d:\\Program Files\\Lua\\5.1\\lua\\?.lua;e:\\LUA_PROJECT\\tr\\src\\?.lua;'--加载标准库require"std"require"table"--加载luaXML库require"LuaXml"local g_alarmInfo = {}--------------------------------------------------------------------- 读取xml文件-- & argument1 文件路径-------------------------------------------------------------------local function readData(filePath)--调用xml.load加载数据local xfile = xml.load(filePath)return xfileend--------------------------------------------------------------分割字符串------------------------------------------------------------local function StringSplit(str,split_char)        -------------------------------------------------------        -- 参数:待分割的字符串,分割字符        -- 返回:子串表.(含有空串)        local sub_str_tab = {};        while (true) do            local pos = string.find(str, split_char);            if (not pos) then                sub_str_tab[#sub_str_tab + 1] = str;                break;            end            local sub_str = string.sub(str, 1, pos - 1);            sub_str_tab[#sub_str_tab + 1] = sub_str;            str = string.sub(str, pos + 1, #str);        end        return sub_str_tab;    end-------------------------------------------------------------解析xml文件----------------------------------------------------------- local function readTestFile(filepath)local xfile = readData(filepath)print(xfile[0])--取size的值,但是只能取到第一个节点的值local item = xfile:find("size")print(item[1])local i = 1local valid_count = 1if xfile ~= nil and xfile[i] ~= nil thenrepeatprint(xfile[i][0])--xfile[i][0] 应该对应IDprint(string.format("value:%s-name:%s.",xfile[i]["value"],xfile[i]["name"]))                        --获取无属性的节点的值print(string.format("size:%d.height:%d.",xfile[i][1][1],xfile[i][2][1]))--取带属性的节点下该属性的值if xfile[i][3] ~= nil and xfile[i][3][0] == "factory" thenprint(string.format("factory name:%s.",xfile[i][3]["name"]))endi = i+1until xfile[i] == nilendendlocal filePath="test_xml.xml"readTestFile(filePath)
输出结果:

原创粉丝点击