使用python替换xml文件中的同级

使用python替换xml文件中的同级,python,xml,replace,find,siblings,Python,Xml,Replace,Find,Siblings,我想要一种在YAMJ的xml文件中更改已观看电影状态的方法。我可以提供baseFilenameBase、fileLocation和fileURL。我想把这两个被看的元素改成真的 <library count="1"> <category count="1" current="true" name="Other"> <index </index> </category> <movies cols="18" c

我想要一种在YAMJ的xml文件中更改已观看电影状态的方法。我可以提供baseFilenameBase、fileLocation和fileURL。我想把这两个被看的元素改成真的

<library count="1">
   <category count="1" current="true" name="Other">
      <index </index>
   </category>
   <movies cols="18" count="18" indexCount="673" totalCount="737">
      <movie isExtra="false" isSet="false" isTV="false">
         <baseFilenameBase>FILE NAME</baseFilenameBase>
         <watched>false</watched>
         <files>
            <file firstPart="1" lastPart="1" season="-1" size="0" subtitlesExchange="NO"
                  title="UNKNOWN"
                  watched="false"
                  zcd="2">
               <fileLocation>PATH TO FILE</fileLocation>
               <fileURL>PATH TO FILE</fileURL>
            </file>
         </files>
      </movie>
      <movie

      And repeats .....
    </movies>
</library>    

与尝试与兄弟姐妹/邻居打交道相比,这非常简单。

以下内容将在所有电影和文件中设置为“已观看”,您应该调整它以仅更改所需的电影和文件,并将movies.xml更改为文件名

from xml.etree import ElementTree

tree = ElementTree.parse('movies.xml')

for movie in tree.findall('movies/movie'):
    movie.find('watched').text = 'true'
    for mfile in movie.findall('files/file'):
        mfile.set('watched', 'true')

tree.write('movies.xml')
这就是我的结局。它确实可以工作,但如果xml在网络共享上,有时会导致播放的媒体断断续续。我相信它是在搜索包含变量的xml时发生的。有没有一种不太需要系统的方式来实现这一点,因为它是在低功耗的媒体播放器上运行的


谢谢

如果您在小型媒体服务器上运行,那么python可能不是答案,因为它应该有
sed

sed -i 's/<watched>false/<watched>true/g' movies.xml
sed-i's/false/true/g'movies.xml

只需检查您拥有的
sed
的哪个版本
sed-h
,就可以知道有哪些选项可用。

假设您的baseFilenameBase是唯一的,一个简单的方法是:我不能评论,所以我不能将其添加到sp的答案中

神奇的是xpath表达式“//movie[baseFilenameBase/text()='PATHHERE']”这是一个xpath表达式,它说:

  • //电影--查找所有电影节点
  • [条件]——条件为真时
  • baseFilenameBase/text()='PATHHERE'--一个条件,表示它必须有一个baseFilenameBase节点,其中包含文本'PATHHERE'
然后我使用format将path变量的内容放入。 XPath是引用xml节点的极好语法

p、 我必须承认我还没有运行过这个。但我不认为有任何语法错误,它应该工作

from xml.etree import ElementTree

tree = ElementTree.parse('movies.xml')
movie = tree.find("//movie[baseFilenameBase/text() = '{}']".format(path))
movie.find('watched').text = 'true'
for file in movie.find('files').findall('file')
    file.set("watched", 'true')

tree.write('movies.xml')

谢谢,这确实将所有更改为true,set命令很好找到。我确实尝试只替换所需的监视文件,方法是在树中的第一个for movie之后添加if unicode(movie.find('baseFilenamebase').text)==myMedia.oStatus.fileName[:-4]:但这不起作用:(我尝试了一些变体,但都不起作用。tree=ElementTree.parse(name)for movie in tree.findall('movies/movie')):penContents=movie.getchildren()用于penContents中的内容:if content.text==myMedia.oStatus.fileName[:-4]:movie.find('wasted')。text='true'用于movie.findall('files/file'):mfile.set('wasted','true')bak_name=name[:-4]+'.bak'树。write(bak_name)os.重命名(bak_name,name)
sed -i 's/<watched>false/<watched>true/g' movies.xml
from xml.etree import ElementTree

tree = ElementTree.parse('movies.xml')
movie = tree.find("//movie[baseFilenameBase/text() = '{}']".format(path))
movie.find('watched').text = 'true'
for file in movie.find('files').findall('file')
    file.set("watched", 'true')

tree.write('movies.xml')