On Fri, 28 Jan 2011, Joaquin Cuenca Abela wrote: +-- | you need to use also the "tail" property. "text" is for the | text inside the element, tail is for the text after the element | is closed. +-- In the lxml/ElementTree world, the way mixed content works is, in my experience, the hardest thing to understand. Perhaps a picture might help: http://www.nmt.edu/tcc/help/pubs/pylxml/etree-view.html Because I came from a DOM background, when I first saw how the .tail attribute works, I completely rejected the entire framework because I thought it was ugly. What changed my mind was performance. With Python's minidom it took one program about 35 seconds to read a half-megabyte XML file; lxml read that same file in 600 milliseconds. Once I started actually using lxml, I found that handling mixed content is not that bad at all. Appended below my .signature is a little function that I use everywhere to append text as the child of an element without having to worry about where it goes. Forgive me for promoting my own work, but the document containing the above link describes how to use lxml for reading, writing, and updating XML. It also includes an annotated version of Fredrik Lundh's builder.py module which makes code to generate XML much more straightforward and compact. http://www.nmt.edu/tcc/help/pubs/pylxml/etree-view.html Best regards, John Shipman (john@nmt.edu), Applications Specialist, NM Tech Computer Center, Speare 119, Socorro, NM 87801, (575) 835-5735, http://www.nmt.edu/~john ``Let's go outside and commiserate with nature.'' --Dave Farber ================================================================ def addText ( node, s ): '''Add text content to an element. [ (node is an Element) and (s is a string) -> if node has any children -> last child's .tail := last child's tail + s else -> node.text := node.text + s ] ''' if len(node) == 0: node.text = (node.text or "") + s else: lastChild = node[-1] lastChild.tail = (lastChild.tail or "") + s