简体   繁体   中英

How to parse and match multiple XML files over multiple iterations with Python?

I have two XML files. One file contains firewall rules which are defined using objects as opposed to IP addresses, services, etc. The second file contains the definition of those objects, their IP addresses, service types, etc.

I can loop through each XML file and match objects to service definitions very easily using ElementTree and the following code:

import xml.etree.ElementTree as ET
fwPolicy = ET.parse('policy.xml')
services = ET.parse('services.xml')

rules = fwPolicy.findall('./rule')
service = services.findall('./service')

for rule in rules:
    ruleServices = rule.findall('./services/members/reference/Name') 
    for ruleService in ruleServices:
            for objService in service: 
                    objServiceName = objService.find('./Name').text 
                    if ruleService.text == objServiceName: 
                            print('Service search Found :', objServiceName)

XML File 1 policy.xml: 

<Class_Name>rule_services</Class_Name> 
  <members>
    <reference>  
        <Name>icmp-requests</Name>        
        <Table>services</Table>
    </reference>
    <reference>  
        <Name>gre</Name>       
        <Table>services</Table>
    </reference>
   </members> 
</Class_Name>



XML File 2 services.xml: 

<service>
 <Name>icmp-requests</Name>
 <Class_Name>service_group</Class_Name>
<members>
    <reference>   
        <Name>echo-request</Name>       
        <Table>services</Table>
    </reference>
    <reference>  
        <Name>info-req</Name>   
        <Table>services</Table>
    </reference>
    <reference>  
        <Name>timestamp</Name>  
        <Table>services</Table>
    </reference>
    <reference>  
        <Name>mask-request</Name>       
        <Table>services</Table>
    </reference>
 </members>
</service> 

<service>
 <Name>echo-request</Name>
 <Class_Name>icmp_service</Class_Name>
 <icmp_type>8</icmp_type>
</service> 

The issue I have is that some of the objects used in policy.xml are "service_groups" which contain the actual services I need to access. In the example XML policy.xml contains "icmp-requests" as the service object which is a "service_group" in services.xml. So when I perform the match I'm matting against the group when I really need to match against all services in the group.

I thought about just adding a code to check if the service object is a service group and then looping though it and pulling all the services. The problem I have is that the service group can contain other service groups. There really is no limit on how far the nesting can occur.

In the end what I need to be able to do is pull all the members from policy.xml and match that to all of the individual members of services.xml.

I was able to fix my problem by building the following function:

def resolve_group_members(group_name):
    # Loop through the list of members
    for member in groups[group_name]:
            # If the member is in the groups dict keys
            if member in groups.keys():
                    # recursively loop through the function again passing the member name
                    resolve_group_members(member)
            else:
                    # if the member is not a group now we can do something with it
                    print('\t\tGroup Members: ' +  member)

This way I was able to assign all services to a dictionary and then resolve each group member.

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