"Gabriel Genellina" <[EMAIL PROTECTED]> wrote in message news:[EMAIL PROTECTED] > En Fri, 21 Sep 2007 11:49:53 -0300, Tim Arnold <[EMAIL PROTECTED]> > escribi�: > >> Hi, I'm using elementtree and elementtidy to work with some HTML files. >> For >> some of these files I need to enclose the body content in a new div tag, >> like this: >> <body> >> <div class="remapped"> >> original contents... >> </div> >> </body> >> >> 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. > > import xml.etree.ElementTree as ET > source = """<html><head><title>Test</title></head><body> > original contents... 2&3 <a href="hello/world">some text</a> > <p>Another paragraph</p> > </body></html>""" > tree = ET.XML(source) > body = tree.find("body") > newdiv = ET.Element('div', {'class':'remapped'}) > newdiv.append(body) > bodyidx = tree.getchildren().index(body) > tree[bodyidx]=newdiv > ET.dump(tree) > > -- > Gabriel Genellina >
The above wraps the body element, not the contents of the body element. I'm no ElementTree expert, but this seems to work: import xml.etree.ElementTree as ET source = """<html><head><title>Test</title></head><body> original contents... 2&3 <a href="hello/world">some text</a> <p>Another paragraph</p> </body></html>""" tree = ET.XML(source) body = tree.find("body") newdiv = ET.Element('div', {'class':'remapped'}) for e in body.getchildren(): newdiv.append(e) newdiv.text = body.text newdiv.tail = body.tail body.clear() body.append(newdiv) ET.dump(tree) Result: <html><head><title>Test</title></head><body><div class="remapped"> original contents... 2&3 <a href="hello/world">some text</a> <p>Another paragraph</p> </div></body></html> -Mark -- http://mail.python.org/mailman/listinfo/python-list