首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何同时解析父级和子级XML节点属性

如何同时解析父级和子级XML节点属性
EN

Stack Overflow用户
提问于 2014-09-26 15:57:54
回答 1查看 49关注 0票数 0

我试图从XML文件中获取特定的属性值,然后将它们放入表中。

以下是XML文件的摘录:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<CUSTOMER defined="true">
<FIREWALLS defined="true">
<CMA display_name="JAPAN" ssh_port="" secondary_host_ip="" collector="Collector_A"      communication="cpstat" Allow_Auto_Implementation="no" secondary_host_name=""   secondary_host_sic_name="" monitoring="yes" audit_from_clm="no" log_collection_frequency="60"   type="CMA" name="" host_name="" use_opsec_data_collection="yes" user_name="" passwd=""   use_opsec_lea="yes" os="linux" defined="true">
        <FW_CKP epasswd="" do_log_analysis="yes" defined="true" original_name="FW_A" name="FW_A"  display_name="FW_A" os="sun" user_name="" passwd="" log_collection_mode="extensive" baseline_profile="" host_name="10.10.10.1" log_server="JAPAN_Pry"/>
        <FW_CKP epasswd="" do_log_analysis="yes" defined="true" original_name="FW_B" name="FW_B" display_name="FW_B" os="sun" user_name="" passwd="" baseline_profile="" log_collection_mode="extensive" host_name="10.10.10.2" log_server="JAPAN_Pry"/>
        <FW_CKP epasswd="" do_log_analysis="yes" defined="true" original_name="FW_C" name="FW_C" display_name="FW_C" os="sun" user_name="" passwd="" baseline_profile="" log_collection_mode="extensive" host_name="10.10.10.3" log_server="JAPAN_Pry"/>
</CMA>
<CMA display_name="USA" ssh_port="" secondary_host_ip="" collector="Collector_B" communication="cpstat" Allow_Auto_Implementation="no" secondary_host_name="" secondary_host_sic_name="" monitoring="yes" audit_from_clm="no" log_collection_frequency="60" type="CMA" name="" host_name="" use_opsec_data_collection="yes" user_name="" passwd="" use_opsec_lea="yes" os="linux" defined="true">
        <FW_CKP epasswd="" do_log_analysis="yes" defined="true" original_name="FW_D" name="FW_D" display_name="FW_D" os="sun" user_name="" passwd="" baseline_profile="" log_collection_mode="extensive" host_name="10.10.10.4" log_server="USA_Pry"/>
</CMA>
</FIREWALLS>
</CUSTOMER>

我需要从CMA节点获取"display_name“和”收集器“值。然后,对于每个CMA节点,我需要为每个original_name子节点获取关联的"log_server“和”FW_CKP“。最后,目的是要有一个表,其中每一行都是这样格式化的: CMA名称-收集器-原始名称-日志服务器名称。

以下是我的当前代码:

代码语言:javascript
复制
' Load XML file
            doc.Load(tb_FilePath.Text)
'XML node path for CMA and FW_CKP
        CMA_Nodes = doc.SelectNodes("/CUSTOMER /FIREWALLS/CMA")
        FW_CKP_Nodes = doc.SelectNodes("/CUSTOMER /FIREWALLS/CMA/FW_CKP")

        'loop to go through each CAM nodes
        For Each CMA_Node As System.Xml.XmlElement In CMA_Nodes

            'operation inside the CMA balise
            CMA_Name = CMA_Node.Attributes(0).InnerText
            CMA_Collector = CMA_Node.Attributes(3).InnerText

            'loop to go through each FW_CKP nodes
            For Each FW_CKP_Node As System.Xml.XmlElement In FW_CKP_Nodes

                'Operation inside the FW_CKP baslise
                Original_Name = FW_CKP_Node.Attributes(3).InnerText
                Log_Server = FW_CKP_Node.Attributes(12).InnerText

                'update the table with the CMA name
                DataGridView1.Rows.Add(New String() {CMA_Name, CMA_Collector, Original_Name, Log_Server})

            Next

        Next

因此,代码将遍历第一个CMA节点,而不是只查看FW_CKP子节点,而是在XML文件中遍历所有这些节点,并在表中放置额外行(错误的行)。

我怎样才能得到以下结果:

收集器-原始名称-日志服务器名称

日本- Collector_A - FW_A - JAPAN_Pry

日本- Collector_A - FW_B - JAPAN_Pry

日本- Collector_A - FW_C - JAPAN_Pry

美国- Collector_B - FW_D - USA_Pry

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-09-26 16:09:10

我认为您的问题是,您是在全局搜索FW_CKP节点,而不是针对每个CMA节点。

但是,如果所有FW_CKP节点都是CMA节点的唯一直接子节点,则只需使用CMA_Node.ChildNodes:

代码语言:javascript
复制
   For Each CMA_Node As System.Xml.XmlElement In CMA_Nodes

        'operation inside the CMA balise
        CMA_Name = CMA_Node.Attributes(0).InnerText
        CMA_Collector = CMA_Node.Attributes(3).InnerText

        'loop to go through each FW_CKP nodes
        For Each FW_CKP_Node As System.Xml.XmlElement In CMA_Node.ChildNodes

            'Operation inside the FW_CKP baslise
            Original_Name = FW_CKP_Node.Attributes(3).InnerText
            Log_Server = FW_CKP_Node.Attributes(12).InnerText

            'update the table with the CMA name
            DataGridView1.Rows.Add(New String() {CMA_Name, CMA_Collector, Original_Name, Log_Server})

        Next

    Next
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/26063623

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档