elementtree question

Fredrik Lundh fredrik at pythonware.com
Wed Sep 26 07:52:24 EDT 2007


Tim Arnold wrote:


> I figure there must be a way to do it by creating a 'div' SubElement to the 
> 'body' tag and somehow copying the rest of the tree under that SubElement, 
> but it's beyond my comprehension.
> 
> How can I accomplish this?
> (I know I could put the class on the body tag itself, but that won't satisfy 
> the powers-that-be).

for completeness, here's an efficient and fairly straightforward way to 
do it under plain 2.5 xml.etree:

body = doc.find(".//body")

# clone and mutate the body element
div = copy.copy(body)
div.tag = "div"
div.set("class", "remapped")

# replace the body contents with the new div
body.clear()
body[:] = [div]

</F>




More information about the Python-list mailing list