简体   繁体   中英

I'm trying to parse an XML file using lua single loop works, but nested loop does not give the expected result

Here is a snippet from my XML file called 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>

I want to print the value of TARGET_TYPE and TARGET_NAME corresponding to a particular tag called NewInstance ref.

I tried to achieve this using two methods but none seems to work. Here are the two approaches :

Lua code 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 code 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()   

I'm new to LUA, please tell me where I'm going wrong.

If X contains your XM data, try this:

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)

Use gmatch when you want to collect all attributes, as in

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

Note the use of single quotes to avoid escaping double quotes.

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()

This code worked and gave the desired attribute values.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM