1

我敢肯定,你们中的许多人都看到了我最近提出的大量小问题。这个周末,我终于放弃并搬到了 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

等等等等,我相信你明白了。

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

谢谢!

4

1 回答 1

1

Both the <Name> and the <Class_Name> are children of the <network_object>. So you could use iterfind to find all the <network_objects>, and then use find to find the single <Name> and <Class_Name> child elements:


For example, with this setup:

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>  
'''

The code

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)

yields

('Test_Group_A', 'network_object_group')
Host1
Host2
Host3
Host4
Host5
Host6
于 2013-03-25T13:51:03.307 回答