Python与etree一起循环

时间:2013-03-25 13:43:24

标签: python xml loops

我相信很多人都看到了我最近遇到过的迷你问题。本周末,我终于放弃并转移到etree,并提出一个简单的问题:我应该如何循环这个以给出以下格式的输出:

名称Class_Name成员颜色#field y #field z

XML示例(有许多不同类型的Class_Name):

<network_objects>
<network_object>
<Name>Test_Group_A</Name>
<Class_Name>network_object_group</Class_Name>
<members>
  <reference>
    <Name>Host1</Name>
    <Table>network_objects</Table>
  </reference>
  <reference>
    <Name>Host2</Name>
    <Table>network_objects</Table>
  </reference>
  <reference>
    <Name>Host3</Name>
    <Table>network_objects</Table>
  </reference>
  <reference>
    <Name>Host4</Name>
    <Table>network_objects</Table>
  </reference>
  <reference>
    <Name>Host5</Name>
    <Table>network_objects</Table>
  </reference>
  <reference>
    <Name>Host6</Name>
    <Table>network_objects</Table>
  </reference>
  <reference>
    <Name>Host7</Name>
    <Table>network_objects</Table>
  </reference>
</members>
<color><![CDATA[deep pink]]></color>
<comments><![CDATA[no comment]]></comments>
<group_convention_query><![CDATA[]]></group_convention_query>
<group_sort_type>3</group_sort_type>
<is_convention_on>false</is_convention_on>
<member_class><![CDATA[network_object]]></member_class>
<members_query><![CDATA[]]></members_query>
<type><![CDATA[group]]></type>
  </network_object>
 </network_objects>

以下代码为我提供了每个名称的一次迭代(我想要的):

for Name in tree.iterfind('network_object/Name'):
    print (Name.text)

但是,如果我在for循环中执行for循环,我会获得所有名称的输出,包含Class_Name的所有变体(与实际情况相比)。例如:

for Name in tree.iterfind('network_object/Name'):
    for Class_name in tree.iterfind('network_object/Class_Name'):
          print (Name.text,Class_name.text)

DB_Servers host_plain
DB_Servers network_object_group
DB_Servers dynamic_object
等等,我相信你明白了。

如何收集我需要的所有数据,然后将每个对象的相关详细信息放在它自己的行上?

谢谢!

1 个答案:

答案 0 :(得分:1)

<Name><Class_Name>都是<network_object>的子女。因此,您可以使用iterfind查找所有<network_objects>,然后使用find查找单个<Name><Class_Name>子元素:


例如,使用此设置:

import xml.etree.ElementTree as ET

content='''<network_objects>
<network_object>
<Name>Test_Group_A</Name>
<Class_Name>network_object_group</Class_Name>
<members>
  <reference>
    <Name>Host1</Name>
    <Table>network_objects</Table>
  </reference>
  <reference>
    <Name>Host2</Name>
    <Table>network_objects</Table>
  </reference>
  <reference>
    <Name>Host3</Name>
    <Table>network_objects</Table>
  </reference>
  <reference>
    <Name>Host4</Name>
    <Table>network_objects</Table>
  </reference>
  <reference>
    <Name>Host5</Name>
    <Table>network_objects</Table>
  </reference>
  <reference>
    <Name>Host6</Name>
    <Table>network_objects</Table>
  </reference>
</members>
</network_object>
</network_objects>  
'''

代码

root = ET.fromstring(content)
for network in root.iterfind('network_object'):
    name = network.find('Name')
    class_name = network.find('Class_Name')
    print (name.text,class_name.text)
    for subname in network.iterfind('members/reference/Name'):
        print(subname.text)

产量

('Test_Group_A', 'network_object_group')
Host1
Host2
Host3
Host4
Host5
Host6