#!/usr/bin/env python # encoding: utf-8 # # Copyright (c) 2010 Doug Hellmann. All rights reserved. # """Creating XML documents with lists of nodes """ #end_pymotw_header from xml.etree.ElementTree import Element, SubElement, tostring, XML from ElementTree_pretty import prettify top = Element('top') parent_a = SubElement(top, 'parent', id='A') parent_b = SubElement(top, 'parent', id='B') # Create children children = XML('''<root><child num="0" /><child num="1" /><child num="2" /></root> ''') # Set the id to the Python object id of the node to make duplicates # easier to spot. for c in children: c.set('id', str(id(c))) # Add to first parent parent_a.extend(children) print 'A:' print prettify(top) print # Copy nodes to second parent parent_b.extend(children) print 'B:' print prettify(top) print