I am trying to parse a large xml file, have tried a bunch of different ways and am stuck. Cant list the XML for security reasons, but I can explain what I am trying to do with the code.
The first for loop looks at our different devices and finds them successfully, the second for loop correctly identifies the rule names specific to each device group.
The problem comes in when I look for the element attached to logname and\or threat name. I want to identify which nodes don't have text assigned to these. I tried some ideas without the try statement, but I constantly got an error when the first one pops that doesn't have a value to that element. Those are exactly what I want to find and report on.
The try statement is screwed up as well, it doesn't run through the loops correctly anymore because I have way too many rule associated with the device groups.
Any help is appreciated much.......audit start tomorrow morning! =]
import xml.etree.ElementTree as ET
tree = ET.parse('/testing/audit/input.xml')
root = tree.getroot()
for dname in root.findall('.//devices/entry/device-group/entry'):
devicename = dname.get('name')
for rules in root.findall('.//post-rulebase/security/rules/entry'):
rulename = rules.get('name')
logname = rules.find('.//post-rulebase/security/rules/entry/log-setting')
threatname = rules.find('.//post-rulebase/security/rules/entry/profile-setting/group/member')
try:
print devicename + " Rule " + rulename + " has log forwarding set to: " + logname.text
print devicename + " Rule " + rulename + " has threat set to: " + threatname.text
break
except:
pass
else:
print devicename + " Rule " + rulename + " needs review"
logname
isNone
andlogname.text
raiseAttributeError
? – Aamir Adnan Jun 25 at 21:12