Python 3.x 使用python解析子级XML
我试图解析下面的XML以获得以下数据。下面有多条规则,我只分享了下面的一条规则。是否可以为这些值解析XML 节标题中的名称、规则id值、应用于名称、源名称、源值、目标名称、目标值Python 3.x 使用python解析子级XML,python-3.x,xml,parsing,elementtree,Python 3.x,Xml,Parsing,Elementtree,我试图解析下面的XML以获得以下数据。下面有多条规则,我只分享了下面的一条规则。是否可以为这些值解析XML 节标题中的名称、规则id值、应用于名称、源名称、源值、目标名称、目标值 <?xml version="1.0" encoding="UTF-8"?> <filteredfirewallConfiguration timestamp="1621338984151"> <contextId>
<?xml version="1.0" encoding="UTF-8"?>
<filteredfirewallConfiguration timestamp="1621338984151">
<contextId>globalroot</contextId>
<layer3Sections>
<section id="asdfsdf" name="production" generationNumber="132" timestamp="1621930404081" managedBy="universalroot-0" tcpStrict="false" stateless="false" useSid="false" type="LAYER3">
<rule id="1213213" disabled="false" logged="true" managedBy="universalroot-0">
<name>From Conversion Server</name>
<action>allow</action>
<appliedToList>
<appliedTo>
<name>re-int-dx</name>
<value>universalwire</value>
<type>VirtualWire</type>
<isValid>true</isValid>
</appliedTo>
<appliedTo>
<name>re-ext-ap</name>
<value>universalwire</value>
<type>VirtualWire</type>
<isValid>true</isValid>
</appliedTo>
</appliedToList>
<sectionId>sfsdfafee</sectionId>
<sources excluded="false">
<source>
<name>sdfsdf101</name>
<value>ipset-werfwefdc</value>
<type>IPSet</type>
<isValid>true</isValid>
</source>
<source>
<name>sdfsfdf102</name>
<value>ipset-4wsetgfreds</value>
<type>IPSet</type>
<isValid>true</isValid>
</source>
</sources>
<destinations excluded="false">
<destination>
<name>production-database-cluster</name>
<value>sg</value>
<type>SecurityGroup</type>
<isValid>true</isValid>
</destination>
<destination>
<name>newname</name>
<value></value>
<type>IPSet</type>
<isValid>true</isValid>
</destination>
</destinations>
<services>
<service>
<name>servicenwe</name>
<value>application-dgfsdfg</value>
<type>Application</type>
<isValid>true</isValid>
</service>
</services>
<direction>inout</direction>
<packetType>any</packetType>
</rule>
到现在为止你都试了些什么?你能分享你的预期结果吗?我能用下面的代码得到完整的章节标题。但仍在努力获取其余细节对于root.find('./layer3Sections'):print(child.tag,child.attrib)``中的子级,预期结果是下面的值,以便将它们导出到csv。节标题中的名称、规则id值、应用于名称、源名称、源值、目标名称、目标值
import requests
import xml.etree.ElementTree as ET
tree = ET.parse("out.xml")
root = tree.getroot()
for child in root.find('./layer3Sections'):
print(child.tag, child.attrib)