我目前正在用python创建一个需要xml操作的项目。为了操作xml文件,我将使用Elementtree。以前从没用过那个模块。我以前用php,但完全不同。在
我有以下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”),然后删除或编辑该视频条目。 示例:
1)搜索标题为“name2”的视频并删除视频条目:
^{pr2}$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>
是的,使用ElementTree可以做到这一点。
.remove()
函数可以从XML树中删除XML元素。下面是一个如何从XML文件中删除名为name2
的所有视频的示例:参考文献:
相关问题 更多 >
编程相关推荐