我使用Lua 5.1.我想解析以下模式的XML文件.我应该怎么做? [英] I use Lua 5.1. I want to parse an XML file of the following pattern. How should I go about it?

查看:119
本文介绍了我使用Lua 5.1.我想解析以下模式的XML文件.我应该怎么做?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我尝试使用LuaXml库.但是它的功能是有限的,因为它仅返回特定属性的第一个子表,并且不会进一步扩展.然后,我尝试了字符串模式匹配,该方法可以正常工作,但是我走到了穷途末路,无法完全完成任务. LuaExpat库位于我的lua的lib文件夹中,并且还存在一个名为lom.lua的文件.但是通常它不起作用,或者给我找不到模块"的错误

I tried using LuaXml library. But its functionality is limited as this returns only the first subtable of a particular attribute and does not go further than that. Then I tried string pattern matching which worked but I reached a dead end and it couldnt completely achieve the task. LuaExpat library is present in my lib folder of lua, and a file called lom.lua is also there. But often it doesnt work or gives me the error that "module not found"

我的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及其对应的目标类型和目标名称,以及与它的关系类型以及与它相关的实例ref的ID,以及其目标类型和目标名称 例如:

My agenda is to display a NewInstance ID and its corresponding target type and target name and also its relation type with and the ID of instance ref its related to, along with its target type and target name for eg:

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无法用于实现此目的.我将在下面列出字符串模式匹配的代码,它可以帮助我完成直到关系类型但不准确的任务

Now LuaXml cannot be used to achieve this. String pattern matching's code I'll list below and it helps me accomplish the task till relation type but not accurately

代码是:

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会让我知道它对我不起作用的可能原因.

Please suggest what approach I should follow to be able to parse the XMl file in the required way. Which in-built library will provide the apt functions. In case you suggest, LuaExpat let me know the possible reasons why it does not work for me.

推荐答案

您不需要任何特殊的库即可在lua中解析xml,有很多内置功能可以编写自己的解析器.

You don't need any special libraries to parse xml in lua, there is plenty of built in functionality to write your own parser.

例如,要检索节点的属性,您可以编写如下内容.

For instance to retrieve the attributes of a node you can write something like this.

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

由于无需写入文件,因此无需打开或关闭文件.

No need to open or close a file, since there is no intent of writing to it.

这篇关于我使用Lua 5.1.我想解析以下模式的XML文件.我应该怎么做?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆