1
如何在每次調用此方法時插入或更新匹配條目?在python的elementTree中插入條目到xml
def makeXml(path):
root = Element("modules")
tree = ElementTree(root)
childPath = Element(os.path.basename(path).split(".")[0])
childPath.set("path", path)
root.append(childPath)
print etree.tostring(root)
當我第一次調用該方法時,它應該創建一個新條目。
makeXml("~/Desktop/filterList.mod")
這第一個版畫<modules><filterList path="~/Desktop/filterList.mod" /></modules>
makeXml("~/Documens/sorter.mod")
但我想,當同樣的方法執行它應該添加一個新的條目像
<modules>
<filterList path="~/Desktop/filterList.mod" />
<sorter path="~/Documens/sorter.mod" />
</modules>
但它沒有發生,相反,它被覆蓋。
完全正常的,但它會增加一個,如果在同一個條目已經存在。 –
@san我已經更新了我的答案。 – mr2ert