Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/lua/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在LuaXML中编辑属性_Xml_Lua_Luaxml - Fatal编程技术网

在LuaXML中编辑属性

在LuaXML中编辑属性,xml,lua,luaxml,Xml,Lua,Luaxml,我已经能够保存和加载XML文件,但我遇到的问题实际上影响了其中的一些文件。下面是我的例子: require 'luaxml' local text = [[ <Viewers> <eaglesfan0251> <Minutes>0</Minutes> <Minutes>eaglesfan0251</Minutes> </eaglesfan0251> <managarmr83&g

我已经能够保存和加载XML文件,但我遇到的问题实际上影响了其中的一些文件。下面是我的例子:

require 'luaxml'

local text = [[
<Viewers>
  <eaglesfan0251>
    <Minutes>0</Minutes>
    <Minutes>eaglesfan0251</Minutes>
  </eaglesfan0251>
  <managarmr83>
    <Minutes>1</Minutes>
    <Minutes>managarmr83</Minutes>
  </managarmr83>
  <gorbatron5000>
    <Minutes>2</Minutes>
  </gorbatron5000>
</Viewers>
]]


local t = xml.eval(text)

for a, b in pairs(t:find("gorbatron5000","Minutes")) do
    if b.TAG ~= nil then
        if b[b.TAG] == "Minutes" then
            print(b[a])
            t:append("Minutes")[a] = "0"
        end
    end
end

print(t)
需要“luaxml”
本地文本=[[
0
鹰队0251
1.
马纳加尔83
2.
]]
本地t=xml.eval(文本)
对于a,b成对(t:find(“gorbatron5000”,“Minutes”))执行
如果b.TAG~=nil,那么
如果b[b.TAG]=“分钟”,则
打印(b[a])
t:附加(“分钟”)[a]=“0”
结束
结束
结束
打印(t)

这会在我试图影响的地点后添加第二个分钟标记。基本上,我希望能够读取会议记录,然后更改它并更新XML。

我可能在这一点上错了,但是“查找您要查找的标记的属性”中的第二个属性不是吗

根据文档,我认为您的代码应该是这样的:

local t = xml.eval(text)

local node = t:find("gorbatron5000")
if node ~= nil then
  print(node[1]) -- Minutes if your first child
  node[1] = 0
end
资料来源: 更详细的信息:

函数xml.find(var、tag、attributeKey、attributeValue) 递归地解析Lua表中与提供的标记>和属性相匹配的子状态

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.
因此,我的理解导致了我编写的代码。
让我知道这是否有帮助,因为我自己也在学习Lua。

我已经尝试过了,它返回2而不是2。我可以解析文本来缩小范围,但我希望有一个更直接的解决方案。我也不知道如何写那个部分,我可以附加另外的2,但我不知道如何将现有的一个改为3。