我想在现有的xml文件中附加一些xml。这是我的功能:
from lxml import etree as ElementTree
import os
import string
import sys
path = "file.xml"
def addEntries(data):
EntitiesExtractionRule = ElementTree.Element("EntitiesExtractionRule", Id=data, Description=data)
TracksideObject= ElementTree.SubElement(EntitiesExtractionRule,"TracksideObject")
TracksideObject.text=data
tree = ElementTree.ElementTree(EntitiesExtractionRule)
tree.write(path)
addEntries("one")
addEntries("two")
问题是当函数addEntries调用tree.write时,xml文件被删除并从头开始重新编写。有一种方法可以将addEntries函数的结果附加到xml文件中,而不会删除现有内容吗?