我有xml文件
<data>
<person>
<Name>xyz</Name>
<add>abc</add>
</person>
</data>
我想添加另一个人节点,比如
^{pr2}$我当前的python代码是
import xml.etree.ElementTree as ET
from xml.etree.ElementTree import Element
from xml.etree.ElementTree import ElementTree
root = ET.parse("Lexicon.xml").getroot()
creRoot = Element("person")
creDictionary = Element("Name")
creDictionary.text = "def"
creRoot.append(creDictionary)
print(ET.tostring(creRoot))
creTree= ElementTree(creRoot)
creTree.write("Lexicon.xml")
当我运行这段代码时,它将创建xml文件,而不是add,结果是
<person>
<Name>def</Name>
</person>
它将删除所有以前的数据。。 好心任何能解决的人。。提前谢谢
子元素用于将节点添加到现有节点:
我们需要
append
新建create元素到各自的父元素。在演示:
注意:最好使用
lxml
b相关问题 更多 >
编程相关推荐