在这里,只有当action =接受需要导航的路径时,我才需要获取和打印源ip地址和目标ip地址的值(从xml文件捕获值是)-
<edge>
<features>
<firewall>-
<firewallRules>
<firewallRule>
action = accept
<source>
<ipAddress>169.25</ipAddress> (need this)
<ipAddress>169.25</ipAddress> (need this
</source>
<destination>
<ipAddress>169.25</ipAddress>(need this)
<ipAddress>169.25</ipAddress>(need this)
</destination>
</firewallRule>
<firewallRule>
------
</firewallRule>
<firewallRule>
-------
</firewallRule>
</features>
</edge>我的xml文件
<edge>
<enableAesni>true</enableAesni>
<enableFips>true</enableFips>
<vseLogLevel>debug</vseLogLevel>
<defaultPolicy>
<action>deny</action>
</defaultPolicy>
<features>
<firewall>
<version>40</version>
<enabled>true</enabled>
<globalConfig>
<tcpPickOngoingConnections>false</tcpPickOngoingConnections>
<enableFtpLooseMode>false</enableFtpLooseMode>
</globalConfig>
<defaultPolicy>
<action>deny</action>
<loggingEnabled>true</loggingEnabled>
</defaultPolicy>
<firewallRules>
<firewallRule>
<id>131074</id>
<description>highAvailability</description>
<action>accept</action>
<source>
<exclude>false</exclude>
<ipAddress>169.25</ipAddress>
<ipAddress>169.25</ipAddress>
</source>
<destination>
<exclude>false</exclude>
<ipAddress>169.25</ipAddress>
<ipAddress>169.25</ipAddress>
<ipAddress>224.81</ipAddress>
</destination>
</firewallRule>
<firewallRule>
<id>131078</id>
<ruleTag>131078</ruleTag>
<name>firewall</name>
<action>accept</action>
<source>
<exclude>false</exclude>
<vnicGroupId>vse</vnicGroupId>
</source>
</firewallRule>
<firewallRule>
<id>137227</id>
<ruleTag>137227</ruleTag>
<name>test_sbbdcn</name>
<action>accept</action>
<source>
<exclude>false</exclude>
<ipAddress>158.87</ipAddress>
</source>
<destination>
<exclude>false</exclude>
<ipAddress>149.131</ipAddress>
</destination>
<application>
<applicationId>application-57</applicationId>
<applicationId>application-105</applicationId>
</application>
</firewallRule>
</features>
<type>gatewayServices</type>
<isUniversal>false</isUniversal>
<hypervisorAssist>false</hypervisorAssist>
<tunnels />我编写的代码未能导航到所需的路径和取值。
import os
from xml.etree import ElementTree
file_name= 'sa1fwir01mci.xml'
full_file = os.path.abspath(os.path.join(file_name))
print(full_file)
dom = ElementTree.parse(full_file)
root = dom.getroot()
print(root)
for node in root:
tag = node.tag
attribute = node.attrib
#print(tag , attribute)
if tag == 'features':
for ab in node.iter('firewallRules'):
print(ab.text)我需要关于如何从xml文件中获得所需的值的帮助。为了清晰起见,请参考路径。只有当action = accept (从上面提到的路径)时,我才需要获取并打印源和目标ip地址的值
发布于 2021-04-20 20:35:33
我知道您正在寻找一种python解决方案,但这里介绍了如何在xsltproc的命令行中使用xslt和xsl模板来实现。在python中,您可以将firewall.xsl的内容用作字符串。
firewall.xsl:
<?xml version="1.0"?>
<xsl:stylesheet version="1.0"
xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:output method="text" />
<xsl:strip-space elements="*" />
<xsl:template match="//firewallRule[
action/text() = 'accept'
]">
<xsl:apply-templates select="source|destination"/>
</xsl:template>
<xsl:template match="source|destination">
<xsl:apply-templates select="ipAddress"/>
</xsl:template>
<xsl:template match="ipAddress">
<xsl:value-of select="." />
<xsl:text>
</xsl:text>
</xsl:template>
<!-- override default built-in text() template -->
<xsl:template match="text()" />
</xsl:stylesheet>命令:
xsltproc firewall.xsl source.xml来源可以在here上找到
https://stackoverflow.com/questions/67158561
复制相似问题