使用多循环和 ET 在 Python 中解析 XML
Parsing XML in Python using Multiple Loops and ET
我正在使用 Python 2.7 编写一个脚本,该脚本与防火墙 API 通信,returns 采用 XML 格式。我希望它找到满足特定条件的所有规则,但我在解析 XML.
时遇到问题
由于我的环境被锁定,我无法使用外部模块。所以我正在使用 urllib2 和 ElementTree
XML(实际XML很大)
<response status="success" code="19">
<result total-count="1" count="1">
<security>
<rules>
<entry name="RULE 1">
<source>
<member>169.254.0.1</member>
<member>169.254.0.2</member>
</source>
<destination>
<member>any</member>
</destination>
</entry>
<entry name="RULE 2">
<source>
<member>169.254.0.3</member>
<member>169.254.0.4</member>
</source>
<destination>
<member>192.168.1.1</member>
</destination>
</entry>
</rules>
</security>
</result>
我想知道是否有任何防火墙的源和目标等于"any." 然后我想报告哪个规则满足这个条件。
我写这篇文章是为了找到所有规则
import urllib2
import xml.etree.ElementTree as ET
url = "https://MyFirewall/api"
response = urllib2.urlopen(url)
html = response.read()
contents = ET.fromstring(html)
#Get the list of rules
rules = []
for item in contents.findall('./result/security/rules/entry'):
rules.append(item.attrib['name'])
此时我的想法是使用此 "rules" 列表来指定“./result/security/rules/entry/rules[x]”或类似内容的 XPATH(可能必须使用 @name)。然后使用 If 条件搜索我要查找的源节点和目标节点。这样我就可以将规则名称与源和目标相关联。
然后我意识到可能有更简单的方法,我想我应该在这里问一下。
谢谢
我想通了。
我缺少的是一种通过在 XML 搜索中使用“[@name='VARIABLE']”来为查询编制索引的方法。
import urllib2
import xml.etree.ElementTree as ET
url = "https://MyFirewall/api"
response = urllib2.urlopen(url)
html = response.read()
contents = ET.fromstring(html)
rules = []
for item in contents.findall('./result/security/rules/entry'):
rules.append(item.attrib['name'])
for rule in rules:
for item in contents.findall(".//*[@name='" + rule + "']/source/member"): #This line is what I was missing
source = item.text
for item in contents.findall(".//*[@name='" + rule + "']/destination/member"): #This line is what I was missing
destination = item.text
if ("any" in source) or ("any" in destination):
print "Rule " + rule + "in Device Group : " + device + "contains an ANY"
#do stuff
我正在使用 Python 2.7 编写一个脚本,该脚本与防火墙 API 通信,returns 采用 XML 格式。我希望它找到满足特定条件的所有规则,但我在解析 XML.
时遇到问题由于我的环境被锁定,我无法使用外部模块。所以我正在使用 urllib2 和 ElementTree
XML(实际XML很大)
<response status="success" code="19">
<result total-count="1" count="1">
<security>
<rules>
<entry name="RULE 1">
<source>
<member>169.254.0.1</member>
<member>169.254.0.2</member>
</source>
<destination>
<member>any</member>
</destination>
</entry>
<entry name="RULE 2">
<source>
<member>169.254.0.3</member>
<member>169.254.0.4</member>
</source>
<destination>
<member>192.168.1.1</member>
</destination>
</entry>
</rules>
</security>
</result>
我想知道是否有任何防火墙的源和目标等于"any." 然后我想报告哪个规则满足这个条件。
我写这篇文章是为了找到所有规则
import urllib2
import xml.etree.ElementTree as ET
url = "https://MyFirewall/api"
response = urllib2.urlopen(url)
html = response.read()
contents = ET.fromstring(html)
#Get the list of rules
rules = []
for item in contents.findall('./result/security/rules/entry'):
rules.append(item.attrib['name'])
此时我的想法是使用此 "rules" 列表来指定“./result/security/rules/entry/rules[x]”或类似内容的 XPATH(可能必须使用 @name)。然后使用 If 条件搜索我要查找的源节点和目标节点。这样我就可以将规则名称与源和目标相关联。
然后我意识到可能有更简单的方法,我想我应该在这里问一下。
谢谢
我想通了。
我缺少的是一种通过在 XML 搜索中使用“[@name='VARIABLE']”来为查询编制索引的方法。
import urllib2
import xml.etree.ElementTree as ET
url = "https://MyFirewall/api"
response = urllib2.urlopen(url)
html = response.read()
contents = ET.fromstring(html)
rules = []
for item in contents.findall('./result/security/rules/entry'):
rules.append(item.attrib['name'])
for rule in rules:
for item in contents.findall(".//*[@name='" + rule + "']/source/member"): #This line is what I was missing
source = item.text
for item in contents.findall(".//*[@name='" + rule + "']/destination/member"): #This line is what I was missing
destination = item.text
if ("any" in source) or ("any" in destination):
print "Rule " + rule + "in Device Group : " + device + "contains an ANY"
#do stuff