[英]Trying to parse a XML file in perl using XML::Simple. But i am not getting expected result
[英]I'm trying to parse an XML file using lua single loop works, but nested loop does not give the expected result
这是我的 XML 文件中的一个片段,名为 oem_1.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"\n", 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 的新手,请告诉我哪里出错了。
如果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
请注意使用单引号以避免转义双引号。
local file = io.open("oem_1.xml", "rb") -- Open file for reading (binary data)
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()
此代码有效并给出了所需的属性值。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.