有一个XML文件生成的视频编辑软件,其中包含剪辑预设的所有数据。 XML对软件有效。它唯一缺少的是fielddominance的元素(我需要在那里设置'upper'值) 文件结构对我来说很难。 这就像这样:
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE xmeml>
<xmeml version="5">
<sequence>
<name>to_color (Resolve)</name>
<duration>121597</duration>
<rate>
<!-- ... -->
</rate>
<in>-1</in>
<out>-1</out>
<timecode>
<!-- ... -->
</timecode>
<media>
<video>
<track>
<clipitem id="983_0121_01_1_5FF70094C4A64669BC77.mov 0">
<name>983_0121_01_1_5FF70094C4A64669BC77.mov</name>
<duration>271</duration>
<rate>
<timebase>25</timebase>
<ntsc>false</ntsc>
</rate>
<start>0</start>
<end>221</end>
<enabled>true</enabled>
<in>25</in>
<out>246</out>
<file id="983_0121_01_1_5FF70094C4A64669BC77.mov 2">
<duration>271</duration>
<rate>
<!-- ... -->
</rate>
<name>983_0121_01_1_5FF70094C4A64669BC77.mov</name>
<pathurl>file://Capture2/Capture2/SHARED/DAVINCI_Render/111.mxf</pathurl>
<timecode>
<!-- ... -->
</timecode>
<media>
<video>
<duration>271</duration>
<samplecharacteristics>
<width>1920</width>
<height>1080</height>
</samplecharacteristics>
</video>
</media>
</file>
<compositemode>normal</compositemode>
<filter>
<enabled>true</enabled>
<start>0</start>
<end>271</end>
<effect>
<name>Opacity</name>
<effectid>opacity</effectid>
<effecttype>motion</effecttype>
<mediatype>video</mediatype>
<effectcategory>motion</effectcategory>
<parameter>
<name>opacity</name>
<parameterid>opacity</parameterid>
<value>100</value>
<valuemin>0</valuemin>
<valuemax>100</valuemax>
</parameter>
</effect>
</filter>
</clipitem>
</track>
<format>
<samplecharacteristics>
<!-- this is what I need to add START -->
<fielddominance>upper</fielddominance>
<!-- this is what I need to add END-->
<width>1920</width>
<height>1080</height>
<pixelaspectratio>square</pixelaspectratio>
<rate>
<!-- ... -->
</rate>
<codec>
<!-- ... -->
</codec>
</samplecharacteristics>
</format>
</video>
<audio>
<track>
<!-- ... -->
</track>
</audio>
</media>
</sequence>
</xmeml>
我需要添加的元素位于sequence - media - video - format - samplecharacteristics
字段中。
但是此标记也存在于sequence - media - video - track - clipitem - file - media - video - samplecharacteristics
我使用xml.etree.ElementTree作为解析器。 这是我正在尝试的,当然我做错了:
import xml.etree.ElementTree as ET
tree = ET.parse(r'\\capture2\11\in.xml')
root = tree.getroot()
fieldd = ET.Element('fielddominance')
fieldd.set('field','upper')
for tag in root.iter('samplecharacteristics'):
tag.append(fieldd)
output_file = r'\\capture2\11\new.xml'
with open(output_file, 'wb') as out:
tree.write(out, encoding='utf-8')
这为每个样本特征标记添加了fielddominance。我不知道如何在标签内设置'upper'值,而不是属性。
我希望仅将<fielddominance>upper</fielddominance>
写入format
代码,但省略<track>
代码
答案 0 :(得分:1)
您可以使用ElementTree的limited XPath support来隔离您要修改的samplecharacteristics
。此外,设置元素的文本只需setting its .text
attribute。
试试这个:
import xml.etree.ElementTree as ET
tree = ET.parse('in.xml')
fieldd = ET.Element('fielddominance')
fieldd.text = 'upper'
for tag in tree.findall("./sequence/media/video/format/samplecharacteristics"):
tag.append(fieldd)
tree.write('new.xml', "UTF-8", True)