how can I select all descendants of a certain element with ElementTree in Python 3.3?

Spaniaunrebra

New Member
This is the sample data.input.xml\[code\]<root> <entry id="1"> <headword>go</headword> <example>I <hw>go</hw> to school.</example></entry></root>\[/code\]I'd like to put node and its descendants into . That is,output.xml\[code\]<root> <entry id="1"> <headword>go</headword> <examplegrp> <example>I <hw>go</hw> to school.</example> </examplegrp></entry></root>\[/code\]My poor and incomplete script is:\[code\]import codecsimport xml.etree.ElementTree as ETfin = codecs.open(r'input.xml', 'rb', encoding='utf-8')data = http://stackoverflow.com/questions/14582297/ET.parse(fin)root = data.getroot()example = root.find('.//example')for elem in example.iter(): ---and then I don't know what to do---\[/code\]
 
Back
Top