2015-10-01 32 views
2

我试过使用LuaXml库。但是它的功能是有限的,因为这只返回特定属性的第一个子表,并且不会超出这个范围。然后我尝试了字符串模式匹配,但是我达到了死胡同,并且它无法完成任务。 LuaExpat库存在于lua的lib文件夹中,还有一个名为lom.lua的文件。但通常不工作,也不给我说:“模块找不到”我使用Lua 5.1。我想分析以下模式的XML文件。我应该怎么做呢?

我的XML文件看起来像这样的错误:

<Service> 
<NewInstance ref="5A"> 
<Std>DiscoveredElement</Std> 
<Key>5A</Key> 
<Attributes> 
<Attribute name="TARGET_TYPE" value="weblogic_cluster" /> 
<Attribute name="DISCOVERED_NAME" value="/Farm_soa4_sys20_soa4_domain/soa4_domain/WSM4_Cluster" /> 
<Attribute name="BROKEN_REASON" value="0" /> 
<Attribute name="TARGET_NAME" value="/Farm_soa4_sys20_soa4_domain/soa4_domain/WSM4_Cluster" /> 
<Attribute name="EMD_URL" value="https://uxsys460.schneider.com:3872/emd/main/" /> 
</Attributes> 
</NewInstance> 

<NewInstance ref="6C"> 
<Std>DiscoveredElement</Std> 
<Key>6C</Key> 
<Attributes> 
<Attribute name="TARGET_TYPE" value="oracle_weblogic_nodemanager" /> 
<Attribute name="SERVICE_TYPE" value=" " /> 
<Attribute name="ORG_ID" value="0" /> 
<Attribute name="TARGET_NAME" value="Oracle WebLogic NodeManager-uxlab090" /> 
</Attributes> 
</NewInstance> 

<NewInstance ref="98"> 
<Std>DiscoveredElement</Std> 
<Key>98</Key> 
<Attributes> 
<Attribute name="TARGET_TYPE" value="composite" /> 
<Attribute name="SERVICE_TYPE" value=" " /> 
<Attribute name="TARGET_NAME" value="SYS-IMG-Grp" /> 
<Attribute name="EMD_URL" value="" /> 
</Attributes> 
</NewInstance> 

<NewRelationship> 
<Parent> 
<Instance ref="98" /> 
</Parent> 
<GenericRelations> 
<Relations type="contains"> 
<Instance ref="5A" /> 
</Relations> 
</GenericRelations> 
</NewRelationship> 

<NewRelationship> 
<Parent> 
<Instance ref="5A" /> 
</Parent> 
<GenericRelations> 
<Relations type="contains"> 
<Instance ref="6C" /> 
</Relations> 
</GenericRelations> 
</NewRelationship> 
<NewRelationship> 
<Parent> 
<Instance ref="5A" /> 
</Parent> 
<GenericRelations> 
<Relations type="contains"> 
<Instance ref="98" /> 
</Relations> 
</GenericRelations> 
</NewRelationship> 
</Service> 

我的议程是显示NewInstance方法ID及其相应的目标类型和目标名称而且它的关系类型与和实例的参考其相关的ID来,与例如它的目标类型和目标名称 沿:

NewInstance ID - 5A 
Target Type - weblogic_cluster 
Target Name - /Farm_soa4_sys20_soa4_domain/soa4_domain/WSM4_Cluster 
Relation Type - contains 
Instance ref - 6C 
Target Type - oracle_weblogic_nodemanager 
Target Name - Oracle WebLogic NodeManager-uxlab090 
Instance ref - 98 
Target Type - composite 
Target Name - SYS-IMG-Grp 

现在LuaXml不能用来实现这一目标。字符串模式匹配的代码,我将在下面列出,它可以帮助我完成这个任务,直到关系类型,但是不能准确地

的代码是:

a={} 
b={} 
c={} 
d={} 
p=0 
i=0 
q=0 

local file = io.open("oem_topology_output.xml", "rb") -- Open file for reading (binary data) 
    for instance in file:read("*a"):gmatch("<NewInstance ref=\"(.-)\">") do 
    a[i] = instance 
    i = i+1 
    end 
file:close() 
local files = io.open("oem_topology_output.xml", "rb") -- Open file for reading (binary data) 
    for instances in files:read("*a"):gmatch("<NewInstance ref=\".-\">(.-)</NewInstance>") do 
    TARGET_TYPE = instances:match('TARGET_TYPE.-value="(.-)"') 
    TARGET_NAME = instances:match('TARGET_NAME.-value="(.-)"') 
    b[p] = TARGET_TYPE 
    c[p] = TARGET_NAME 
    p =p+1 
    end 
local file = io.open("oem_topology_output.xml", "rb") -- Open file for reading (binary data) 
    for type in file:read("*a"):gmatch("<Relations type=\"(.-)\">") do 
    d[q] = type 
    q = q+1 
    end 
files:close() 
for j=0,i-1 do 
print("INSTANCE ID : ", a[j]) 
print("TARGET TYPE : ", b[j]) 
print("TARGET NAME : ", c[j]) 
print("RELATION TYPE : ",d[j]) 
end 

请建议我应该遵循什么样的办法才能够解析XMl文件以所需的方式。哪个内置库将提供apt功能。如果您建议,LuaExpat让我知道为什么它不适合我的可能原因。

+2

读取文件内容只有一次:'本地XML = io.open( “oem_topology_output.xml”, “RB”):读取( “*一”)'然后每次搜索时都使用'xml:gmatch'。 –

+0

@EgorSkriptunoff非常感谢!实施了。 –

+0

'用于inst_id,xml中的rel_type:gmatch' .-

回答

1

你不需要任何特殊的库来解析lua中的xml,有很多内置的功能来编写你自己的解析器。

例如,要检索一个节点的属性,你可以写这样的东西。

local file = "oem_topology_output.xml" 
local node = "<(%a-)%s* " 
local attributes = { 
    "ref", 
    "name", 
    "value", 
    "type" 
} 


for line in io.lines(file) do 
    for a in line:gmatch(node) do 
     for x = 1, #attributes do 
      n = line:match(attributes[x]..'="(.-)"') 
      if n then 
       print(a, attributes[x], n) 
      end 
     end 
    end 
end 

将会产生像

NewInstance ref 5A 
Attribute name TARGET_TYPE 
Attribute value weblogic_cluster 
Attribute name DISCOVERED_NAME 
Attribute value /Farm_soa4_sys20_soa4_domain/soa4_domain/WSM4_Cluster 
Attribute name BROKEN_REASON 
Attribute value 0 
Attribute name TARGET_NAME 
Attribute value /Farm_soa4_sys20_soa4_domain/soa4_domain/WSM4_Cluster 
Attribute name EMD_URL 
Attribute value https://uxsys460.schneider.com:3872/emd/main/ 
NewInstance ref 6C 
Attribute name TARGET_TYPE 
Attribute value oracle_weblogic_nodemanager 
Attribute name SERVICE_TYPE 
Attribute value  
Attribute name ORG_ID 
Attribute value 0 
Attribute name TARGET_NAME 
Attribute value Oracle WebLogic NodeManager-uxlab090 
NewInstance ref 98 
Attribute name TARGET_TYPE 
Attribute value composite 
Attribute name SERVICE_TYPE 
Attribute value  
Attribute name TARGET_NAME 
Attribute value SYS-IMG-Grp 
Attribute name EMD_URL 
Attribute value 
Instance ref 98 
Relations type contains 
Instance ref 5A 
Instance ref 5A 
Relations type contains 
Instance ref 6C 
Instance ref 5A 
Relations type contains 
Instance ref 98 

输出无需打开或关闭文件,因为没有写它的意图。

0
local a, b, c, d = {}, {}, {}, {} 

local h = io.open("oem_topology_output.xml", "rb") 
if not h then return end 
local txt = h:read("*a") 
h:close() 

for ref, instances in txt:gmatch('<NewInstance ref="(%w+)">(.-)</NewInstance>') do 
    a[#a+1] = ref 
    TARGET_TYPE = instances:match('name="TARGET_TYPE"%s+value="(.-)"') 
    if TARGET_TYPE then b[#b+1] = TARGET_TYPE end 
    TARGET_NAME = instances:match('name="TARGET_NAME"%s+value="(.-)"') 
    if TARGET_NAME then c[#c+1] = TARGET_NAME end 
end 

for relationship in txt:gmatch('<NewRelationship>(.-)</NewRelationship>') do 
    parent = relationship:match('<Parent>.-ref="(%w+)".-</Parent>') 
    node = relationship:match('<Relations type="contains">.-ref="(%w+)".-</Relations>') 
    if parent and node then d[#d+1] = { [parent] = node } end 
end 

for j=1,#a do 
    local id = a[j] 
    print("INSTANCE ID : ", id) 
    print("TARGET TYPE : ", b[j]) 
    print("TARGET NAME : ", c[j]) 
    for k,v in pairs(d) do 
     if type(v)=='table' and v[id] then 
      print("\tRELATION: ",v[id]) 
     end 
    end 
end 

输出:

INSTANCE ID : 5A 
TARGET TYPE : weblogic_cluster 
TARGET NAME : /Farm_soa4_sys20_soa4_domain/soa4_domain/WSM4_Cluster 
    RELATION: 6C 
    RELATION: 98 
INSTANCE ID : 6C 
TARGET TYPE : oracle_weblogic_nodemanager 
TARGET NAME : Oracle WebLogic NodeManager-uxlab090 
INSTANCE ID : 98 
TARGET TYPE : composite 
TARGET NAME : SYS-IMG-Grp 
    RELATION: 5A 
相关问题