I have the following XML
<Head>
<child></child>
</Head>
And I want to alter the XML to something like this:
<Parent>
<child></child>
</Parent>
How do I do this? I have read this and this but they use Element tree.
I have the following XML
<Head>
<child></child>
</Head>
And I want to alter the XML to something like this:
<Parent>
<child></child>
</Parent>
How do I do this? I have read this and this but they use Element tree.
For instance, you have this original file.
<Head>
<child>this is text for child</child>
</Head>
Then you can use xml.dom.minidom to change name of root tag.
#-*- coding:utf-8 -*-
#!/usr/bin/env python
import xml.dom.minidom
info = '''
<Head>
<child>this is text for child</child>
</Head>
'''
dom = xml.dom.minidom.parseString(info)
dom.firstChild.tagName = 'parent'
# save it to any file you want
xml_file = 'C:\\temp\\lch.xml'
f = open(xml_file, 'wb')
dom.writexml(f)
f.close()
OUTPUT:
<?xml version="1.0" ?><parent>
<child>this is text for child</child>
</parent>