0
我目前正在python中創建一個需要xml操作的項目。要操縱xml文件,我將使用Elementtree。之前從未使用該模塊。我曾經使用PHP,但是完全不同。Python Elementtree刪除/編輯節點
我有以下XML文件:
<myvideos>
<video>
<title>video1</title>
<plot>description bla bla bla</plot>
<duration>50</duration>
</video>
<video>
<title>name2</title>
<plot>another description bla bla bla</plot>
<duration>37</duration>
</video>
<video>
<title>another name etc</title>
<plot>description etc...</plot>
<duration>99</duration>
</video>
</myvideos>
我想要做的是視頻標題搜索,(對於爲例「NAME2」),然後刪除或編輯該視頻條目。 Exemples:
1)搜索標題爲 「NAME2」 的視頻和刪除視頻條目:
<myvideos>
<video>
<title>video1</title>
<plot>description bla bla bla</plot>
<duration>50</duration>
</video>
<video>
<title>another name etc</title>
<plot>description etc...</plot>
<duration>99</duration>
</video>
</myvideos>
2)搜索標題爲 「NAME2」 的視頻和編輯項:
<myvideos>
<video>
<title>video1</title>
<plot>description bla bla bla</plot>
<duration>50</duration>
</video>
<video>
<title>name2renamed</title>
<plot>edited</plot>
<duration>9999</duration>
</video>
<video>
<title>another name etc</title>
<plot>description etc...</plot>
<duration>99</duration>
</video>
</myvideos>
歡迎堆棧溢出!本網站是一個問答網站。讀者,比如你自己,提出問題和其他讀者試圖回答他們。我注意到你的帖子丟失了一個問題。你有特定的問題要問嗎? (另外,您可能需要查看[幫助中心](https://stackoverflow.com/help)。) –
對不起,我是新來的。我想問一下使用ElementTree是否可以做到這一點。 – Duoramy
是的,這是可能的。我將首先閱讀ElementTree教程,特別是標題爲「修改XML文件」的部分。 https://docs.python.org/2/library/xml.etree.elementtree.html#modifying-an-xml-file –