提供一个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]即可。
--------------------------------------------------------------------
--记录下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 xfile
end
------------------------------------------------------------
--分割字符串
------------------------------------------------------------
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 = 1
local valid_count = 1
if xfile ~= nil and xfile[i] ~= nil then
repeat
print(xfile[i][0])--xfile[i][0] 应该对应ID
print(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" then
print(string.format("factory name:%s.",xfile[i][3]["name"]))
end
i = i+1
until xfile[i] == nil
end
end
local filePath="test_xml.xml"
readTestFile(filePath)
输出结果: