我正在尝试使用Lua解析XML文件,单层循环可以工作,但嵌套循环不能给出预期的结果。

这是我的名为oem_1.xml的XML文件的片段:

<Service>
<NewInstance ref="a39d725e7689b99e91af6fcb68bc5ec2">
<Std>DiscoveredElement</Std>
<Key>a39d725e7689b99e91af6fcb68bc5ec2</Key>
</NewInstance>
<NewRelationship>
<Parent>
<Instance ref="a39d725e7689b99e91af6fcb68bc5ec2" />
</Parent>
<Components>
<Instance ref="E0246C56D81A7A79559669CD16A8B555" />
<Instance ref="2D5481A0EA3F81AC1E06E2C32231F41B" />
</Components>
</NewRelationship>
<NewInstance ref="E961625723F5FDC8BD550077282E074C">
<Std>DiscoveredElement</Std>
<Key>E961625723F5FDC8BD550077282E074C</Key>
<Attributes>
<Attribute name="TARGET_TYPE" value="j2ee_application" />
<Attribute name="AppType" value="ear" />
<Attribute name="TARGET_GUID" value="E961625723F5FDC8BD550077282E074C" />
<Attribute name="TARGET_NAME" value="/Farm_b2b4_sys20_b2b4_domain/b2b4_domain/WLS_B2B4a/worklistapp" />
</Attributes>
</NewInstance>
<NewInstance ref="FD8A116D5C8DD2332B024BCBD6A81BD8">
<Std>DiscoveredElement</Std>
<Key>FD8A116D5C8DD2332B024BCBD6A81BD8</Key>
<Attributes>
<Attribute name="TARGET_TYPE" value="composite" />
<Attribute name="SERVICE_TYPE" value="" />
<Attribute name="TARGET_NAME" value="LAB-DB-B-AIX-Grp" />
<Attribute name="TARGET_GUID" value="FD8A116D5C8DD2332B024BCBD6A81BD8" />
</Attributes>
</NewInstance>
</Service> 

我想要打印与名为NewInstance ref的特定标签相对应的TARGET_TYPE和TARGET_NAME的值。

我尝试使用两种方法实现,但似乎都不起作用。以下是这两种方法:

Lua代码1-

local file = io.open("oem_1.xml", "rb")
for instance, name, value in file:read("*a"):gmatch("<NewInstance ref=\"(E961625723F5FDC8BD550077282E074C)\"  />"):gmatch("<Attribute name=\"(TARGET_NAME)\" value=\"(.-)\" />"):gmatch("<Attribute name=\"(TARGET_TYPE)\" value=\"(.-)\" />")
do
    print(string.format("Instance: %s", instance))
    print(string.format("Name: %s\nValue: %s", name, value))
end
file:close()

Lua代码2-

local file = io.open("oem_1.xml", "rb")
for instance in file:read("*a"):gmatch("<NewInstance ref=\"(E961625723F5FDC8BD550077282E074C)\"  />")
do
   for name, value in file:read("*a"):gmatch("<Attribute name=\"(TARGET_NAME)or(TARGET_TYPE)\" value=\"(.-)\" />")
    do
        print(string.format("Name: %s\nValue: %s", name, value))
    end
        print(string.format("Instance: %s", instance))
  end
file:close()

我是LUA的新手,请告诉我我错在哪里。

点赞
用户107090
用户107090

如果 X 包含你的 XM 数据,请尝试以下操作:

instance = X:match('<NewInstance ref="E961625723F5FDC8BD550077282E074C">(.-)</NewInstance>')
TARGET_TYPE = instance:match('TARGET_TYPE.-value="(.-)"')
TARGET_NAME = instance:match('TARGET_NAME.-value="(.-)"')
print(TARGET_TYPE)
print(TARGET_NAME)

当您想要收集所有属性时,请使用 gmatch,如下所示:

for k,v in instance:gmatch('<Attribute name="(.-)".-value="(.-)"') do
    print(k,v)
end

请注意,使用单引号可以避免转义双引号。

2015-09-18 12:10:09
用户5336587
用户5336587
local file = io.open("oem_1.xml", "rb")   -- 打开二进制文件用于读取

instance = file:read("*a"):match('<NewInstance ref="E961625723F5FDC8BD550077282E074C">(.-)</NewInstance>')
TARGET_TYPE = instance:match('TARGET_TYPE.-value="(.-)"')
TARGET_NAME = instance:match('TARGET_NAME.-value="(.-)"')
print(TARGET_TYPE)
print(TARGET_NAME)

file:close()

这段代码有效并且给出了所需的属性值。

2015-09-21 03:38:56