xml.etree.ElementTree修改xml遇到空节点
我想把如下的xml中的Height值改为1,
<Student >
<Id>1</Id>
<Age></Age>
<Height>0</Height>
<URI></URI>
</Student >
python代码如下:
##########################################################
import os
import xml.etree.ElementTree as ET
tree = ET.parse('config.xml')
root = tree.getroot()
tag = root.tag
for Height in root.iter('Height'):
new_Height = 1
Height.text = str(new_Height )
tree.write('config.xml')
结果Height的值是修改好了,
但是发现节点为空的地方被强制删掉了一些东西:
<Age></Age>变成了<Age/>
<URI></URI>变成了<URI />
这个该怎么办呢?