我想將整個標記添加到xml,下面是我的XML格式。python:在xml中添加新數據會覆蓋現有數據
<?xml version="1.0" encoding="UTF-8"?>
<ca st="true" name="XMLConfig">
<app>
<!--- I want to add entire commneted tag to XML . !
<ar ty="co" name="st">
<ly ty="pt">
<pt>value</pt>
</Layout>
</ar> -->
<roll name="roll" fN="file.log" fP="logs.gz">
<ly type="ptl">
<pt>value</pt>
</ly>
<po>
<!-- Comment /> -->
<si size="100 MB" />
<!-- Comment /> -->
</po>
<de fI="max" max="10"/>
</roll>
</app>
如上述文件中顯示我想在文件中添加此標記
<ar ty="co" name="st">
<ly ty="pt">
<pt>value</pt>
</Layout>
</ar>
這是我迄今達成..
for appenders in tree.xpath('//Appenders'):
if appenders.getchildren():
appenders.remove(appenders.getchildren()[0])
appenders.insert(0, appenders.getparent().append(etree.fromstring('<ar ty="co" name="st"> <ly ty="pt"><pt>value</pt></Layout></ar>')))
這是消除畢竟其他內容新的內容。 任何幫助將不勝感激。!
同意,我回去檢查了這一點。第一種方法是按照它應該的方式工作的。謝謝回覆,! 第一:因爲XML文件有超過200行..我認爲所有這些細節是不需要的。 第2次:從現在開始,我會照顧這個關鍵詞..! – tgcloud
是的,我做的第一個方法是給我appenders.insert(0,無)並拋出異常。但我發誓它之前一直在爲我工作(刪除新內容後的所有其他內容)。我想我在發佈代碼的時候搞砸了一些東西(我根本不應該對變量進行更改) – tgcloud