python - trying to add element to xmeml format XML with xml.etree.ElementTree -


there's xml file generated video editing software, contains data clip exhange. xml valid software. thing lacks element fielddominance (i need set 'upper' value there) hard part me file structure. here's looks like:

<?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>                 <!-- need add start -->                 <fielddominance>upper</fielddominance>                  <!-- need 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> 

the element need add in sequence - media - video - format - samplecharacteristics field. tag exists in sequence - media - video - track - clipitem - file - media - video - samplecharacteristics

i use xml.etree.elementtree parser. here's trying, , of course i'm doing wrong:

import xml.etree.elementtree et tree = et.parse(r'\\capture2\11\in.xml') root = tree.getroot() fieldd = et.element('fielddominance') fieldd.set('field','upper') tag in root.iter('samplecharacteristics'):     tag.append(fieldd) output_file  = r'\\capture2\11\new.xml' open(output_file, 'wb') out:     tree.write(out, encoding='utf-8') 

this adds fielddominance every samplecharacteristics tag. , have no idea how set 'upper' value inside tag, not attribute.

what write <fielddominance>upper</fielddominance> format tag, omit <track> tags

you can use elementtree's limited xpath support isolate samplecharacteristics want modify. also, setting text of element done setting .text attribute.

try this:

import xml.etree.elementtree et  tree = et.parse('in.xml') fieldd = et.element('fielddominance') fieldd.text = 'upper' tag in tree.findall("./sequence/media/video/format/samplecharacteristics"):     tag.append(fieldd) tree.write('new.xml', "utf-8", true) 

Comments

Popular posts from this blog

python - How to insert QWidgets in the middle of a Layout? -

python - serve multiple gunicorn django instances under nginx ubuntu -

module - Prestashop displayPaymentReturn hook url -