Python Looping with etree

ptthanh

New Member
I'm sure many of you have seen the inundation of minidom questions I've had as of late. This weekend, I finally gave up and moved to etree, and have one simple question: How should I be looping this to give the output in the following format:Name Class_Name members color #field y #field zXML example (there are many different types of Class_Name):\[code\]<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>\[/code\]The following code gives me one iteration of each name (what I want):\[code\]for Name in tree.iterfind('network_object/Name'): print (Name.text)\[/code\]However, if I do a for loop within the for loop, I get an output of all names, with all variations of Class_Name (versus what it is in reality). For instance:\[code\]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_plainDB_Servers network_object_groupDB_Servers dynamic_object\[/code\]etc etc, I'm sure you get the idea. How would I go about collecting all of the data I need, and then placing each object with it's relevant details, on it's own line?Thank you!
 
Back
Top