#!/usr/bin/env python # encoding: utf-8 # # Copyright (c) 2010 Doug Hellmann. All rights reserved. # """Creating XML documents """ #end_pymotw_header import sys from xml.etree.ElementTree import Element, SubElement, Comment, ElementTree top = Element('top') comment = Comment('Generated for PyMOTW') top.append(comment) child = SubElement(top, 'child') child.text = 'This child contains text.' child_with_tail = SubElement(top, 'child_with_tail') child_with_tail.text = 'This child has regular text.' child_with_tail.tail = 'And "tail" text.' child_with_entity_ref = SubElement(top, 'child_with_entity_ref') child_with_entity_ref.text = 'This & that' empty_child = SubElement(top, 'empty_child') ElementTree(top).write(sys.stdout)