首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >XML大文件处理

XML大文件处理
EN

Stack Overflow用户
提问于 2021-04-19 16:32:51
回答 1查看 57关注 0票数 1

在这里,只有当action =接受需要导航的路径时,我才需要获取和打印源ip地址和目标ip地址的值(从xml文件捕获值是)-

代码语言:javascript
复制
<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文件

代码语言:javascript
复制
<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 />

我编写的代码未能导航到所需的路径和取值。

代码语言:javascript
复制
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地址的值

EN

回答 1

Stack Overflow用户

发布于 2021-04-20 20:35:33

我知道您正在寻找一种python解决方案,但这里介绍了如何在xsltproc的命令行中使用xslt和xsl模板来实现。在python中,您可以将firewall.xsl的内容用作字符串。

firewall.xsl:

代码语言:javascript
复制
<?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>&#x0a;</xsl:text>
    </xsl:template>

    <!-- override default built-in text() template -->
    <xsl:template match="text()" />
</xsl:stylesheet>

命令:

代码语言:javascript
复制
xsltproc firewall.xsl source.xml

来源可以在here上找到

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

https://stackoverflow.com/questions/67158561

复制
相关文章

相似问题

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