xml - Python Looping with etree -


i'm sure many of have seen inundation of minidom questions i've had of late. weekend, gave , moved etree, , have 1 simple question: how should looping give output in following format:

name class_name members color #field y #field z

xml example (there many different types of 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> 

the following code gives me 1 iteration of each name (what want):

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

however, if loop within loop, output of names, variations of class_name (versus in reality). instance:

for name in tree.iterfind('network_object/name'):     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 

etc etc, i'm sure idea.

how go collecting of data need, , placing each object it's relevant details, on it's own line?

thank you!

both <name> , <class_name> children of <network_object>. use iterfind find <network_objects>, , use find find single <name> , <class_name> child elements:


for example, setup:

import xml.etree.elementtree 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) network in root.iterfind('network_object'):     name = network.find('name')     class_name = network.find('class_name')     print (name.text,class_name.text)     subname in network.iterfind('members/reference/name'):         print(subname.text) 

yields

('test_group_a', 'network_object_group') host1 host2 host3 host4 host5 host6 

Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -