I have a sample xml file like this:
<root>
She
<opt>went</opt>
<opt>didn't go</opt>
to school.
</root>
I want to create a subelement named of , and put all the contents of into it. That is,
<root>
<sentence>
She
<opt>went</opt>
<opt>didn't go</opt>
to school.
</sentence>
</root>
I know hot to make a subelement with ElementTree or lxml, but I have no idea of how to select from “She” to “shools.” all at once.
import lxml.etree as ET
ET.SubElement(root, 'sentence')
I'm lost...
You could go about it in reverse: (Instead of adding a subelement, add a new parent.) By that I mean, change the
roottag tosentence, create a newrootelement, and insert the oldroot(nowsentence) into the newroot: