title: How to alter 2 subelements of xml file in python by their tag parameter using ElementTree?
body: <p>I have an xml file like this, I want to alter the 2 sub-elements.</p>
  <p>I tried like below.</p>
  <pre><code>import xml.etree.ElementTree as ET
  
  file_name = 'manifest.mpd'
  tree = ET.parse(file_name)
  root = tree.getroot()
  
  for elem in root:
      for subelem in elem:
          a = subelem.attrib
          if a['mimeType'] == 'audio/mp4':
              if subelem.get('lang') == 'bg':
                  source_elem = subelem
                  break
  
  for elem in root:
      for subelem in elem:
          d = subelem.attrib
          if d['mimeType'] == 'audio/mp4':
              if (subelem.get('lang') == 'tr'):
                  subelem = source_elem
  
  tree.write('output.mpd')
  </code></pre>
  <p>Below is my xml file.</p>
  <pre><code>      &lt;?xml version=&quot;1.0&quot; ?&gt;
        &lt;MPD xmlns=&quot;urn:mpeg:dash:schema:mpd:2011&quot; profiles=&quot;urn:mpeg:dash:profile:isoff-live:2011&quot; minBufferTime=&quot;PT4.80S&quot; mediaPresentationDuration=&quot;PT44M19.080S&quot; type=&quot;static&quot;&gt;
          &lt;Period&gt;
            &lt;AdaptationSet mimeType=&quot;video/mp4&quot; segmentAlignment=&quot;true&quot; startWithSAP=&quot;1&quot; maxWidth=&quot;1920&quot; maxHeight=&quot;1080&quot;&gt;
              &lt;SegmentTemplate timescale=&quot;1000&quot; duration=&quot;4800&quot; initialization=&quot;$RepresentationID$/init.mp4&quot; media=&quot;$RepresentationID$/seg-$Number$.m4s&quot; startNumber=&quot;1&quot;/&gt;
              &lt;Representation id=&quot;video/avc1/1&quot; codecs=&quot;avc1.64001E&quot; width=&quot;640&quot; height=&quot;360&quot; scanType=&quot;progressive&quot; frameRate=&quot;25&quot; bandwidth=&quot;798779&quot;/&gt;
              &lt;Representation id=&quot;video/avc1/2&quot; codecs=&quot;avc1.640028&quot; width=&quot;1920&quot; height=&quot;1080&quot; scanType=&quot;progressive&quot; frameRate=&quot;25&quot; bandwidth=&quot;4107000&quot;/&gt;
              &lt;Representation id=&quot;video/avc1/3&quot; codecs=&quot;avc1.64001E&quot; width=&quot;848&quot; height=&quot;480&quot; scanType=&quot;progressive&quot; frameRate=&quot;25&quot; bandwidth=&quot;1498236&quot;/&gt;
              &lt;Representation id=&quot;video/avc1/4&quot; codecs=&quot;avc1.64001F&quot; width=&quot;1280&quot; height=&quot;720&quot; scanType=&quot;progressive&quot; frameRate=&quot;25&quot; bandwidth=&quot;2497736&quot;/&gt;
            &lt;/AdaptationSet&gt;
            &lt;AdaptationSet mimeType=&quot;audio/mp4&quot; startWithSAP=&quot;1&quot; segmentAlignment=&quot;true&quot; lang=&quot;en&quot;&gt;
              &lt;SegmentTemplate timescale=&quot;1000&quot; duration=&quot;2000&quot; initialization=&quot;$RepresentationID$/init.mp4&quot; media=&quot;$RepresentationID$/seg-$Number$.m4s&quot; startNumber=&quot;1&quot;/&gt;
              &lt;Representation id=&quot;audio/en/mp4a.40.2&quot; codecs=&quot;mp4a.40.2&quot; bandwidth=&quot;131529&quot; audioSamplingRate=&quot;48000&quot;&gt;
                &lt;AudioChannelConfiguration schemeIdUri=&quot;urn:mpeg:mpegB:cicp:ChannelConfiguration&quot; value=&quot;2&quot;/&gt;
              &lt;/Representation&gt;
            &lt;/AdaptationSet&gt;
            &lt;AdaptationSet mimeType=&quot;audio/mp4&quot; startWithSAP=&quot;1&quot; segmentAlignment=&quot;true&quot; lang=&quot;tr&quot;&gt;
              &lt;SegmentTemplate timescale=&quot;1000&quot; duration=&quot;4800&quot; initialization=&quot;$RepresentationID$/init.mp4&quot; media=&quot;$RepresentationID$/seg-$Number$.m4s&quot; startNumber=&quot;1&quot;/&gt;
              &lt;Representation id=&quot;audio/tr/mp4a.40.2&quot; codecs=&quot;mp4a.40.2&quot; bandwidth=&quot;127472&quot; audioSamplingRate=&quot;44100&quot;&gt;
                &lt;AudioChannelConfiguration schemeIdUri=&quot;urn:mpeg:mpegB:cicp:ChannelConfiguration&quot; value=&quot;2&quot;/&gt;
              &lt;/Representation&gt;
            &lt;/AdaptationSet&gt;
            &lt;AdaptationSet mimeType=&quot;audio/mp4&quot; startWithSAP=&quot;1&quot; segmentAlignment=&quot;true&quot; lang=&quot;ru&quot;&gt;
              &lt;SegmentTemplate timescale=&quot;1000&quot; duration=&quot;2000&quot; initialization=&quot;$RepresentationID$/init.mp4&quot; media=&quot;$RepresentationID$/seg-$Number$.m4s&quot; startNumber=&quot;1&quot;/&gt;
              &lt;Representation id=&quot;audio/ru/mp4a.40.2&quot; codecs=&quot;mp4a.40.2&quot; bandwidth=&quot;129917&quot; audioSamplingRate=&quot;48000&quot;&gt;
                &lt;AudioChannelConfiguration schemeIdUri=&quot;urn:mpeg:mpegB:cicp:ChannelConfiguration&quot; value=&quot;2&quot;/&gt;
              &lt;/Representation&gt;
            &lt;/AdaptationSet&gt;
            &lt;AdaptationSet mimeType=&quot;audio/mp4&quot; startWithSAP=&quot;1&quot; segmentAlignment=&quot;true&quot; lang=&quot;bg&quot;&gt;
              &lt;SegmentTemplate timescale=&quot;1000&quot; duration=&quot;2000&quot; initialization=&quot;$RepresentationID$/init.mp4&quot; media=&quot;$RepresentationID$/seg-$Number$.m4s&quot; startNumber=&quot;1&quot;/&gt;
              &lt;Representation id=&quot;audio/bg/mp4a.40.2&quot; codecs=&quot;mp4a.40.2&quot; bandwidth=&quot;135019&quot; audioSamplingRate=&quot;48000&quot;&gt;
                &lt;AudioChannelConfiguration schemeIdUri=&quot;urn:mpeg:mpegB:cicp:ChannelConfiguration&quot; value=&quot;2&quot;/&gt;
              &lt;/Representation&gt;
            &lt;/AdaptationSet&gt;
            &lt;AdaptationSet mimeType=&quot;text/vtt&quot; contentType=&quot;text&quot; lang=&quot;en&quot;&gt;
              &lt;Role schemeIdUri=&quot;urn:mpeg:dash:role:2011&quot; value=&quot;subtitle&quot;/&gt;
              &lt;Representation id=&quot;subtitles/en&quot; bandwidth=&quot;141&quot;&gt;
                &lt;BaseURL&gt;subtitles/en/TDCS00187_SPY001285-00001ENG.vtt&lt;/BaseURL&gt;
              &lt;/Representation&gt;
            &lt;/AdaptationSet&gt;
            &lt;AdaptationSet mimeType=&quot;text/vtt&quot; contentType=&quot;text&quot; lang=&quot;sr&quot;&gt;
              &lt;Role schemeIdUri=&quot;urn:mpeg:dash:role:2011&quot; value=&quot;subtitle&quot;/&gt;
              &lt;Representation id=&quot;subtitles/sr&quot; bandwidth=&quot;130&quot;&gt;
                &lt;BaseURL&gt;subtitles/sr/TDCS00187_SPY001285-00001SRB.vtt&lt;/BaseURL&gt;
              &lt;/Representation&gt;
            &lt;/AdaptationSet&gt;
            &lt;AdaptationSet mimeType=&quot;text/vtt&quot; contentType=&quot;text&quot; lang=&quot;hr&quot;&gt;
              &lt;Role schemeIdUri=&quot;urn:mpeg:dash:role:2011&quot; value=&quot;subtitle&quot;/&gt;
              &lt;Representation id=&quot;subtitles/hr&quot; bandwidth=&quot;132&quot;&gt;
                &lt;BaseURL&gt;subtitles/hr/TDCS00187_SPY001285-00001CRO.vtt&lt;/BaseURL&gt;
              &lt;/Representation&gt;
            &lt;/AdaptationSet&gt;
            &lt;AdaptationSet mimeType=&quot;text/vtt&quot; contentType=&quot;text&quot; lang=&quot;sq&quot;&gt;
              &lt;Role schemeIdUri=&quot;urn:mpeg:dash:role:2011&quot; value=&quot;subtitle&quot;/&gt;
              &lt;Representation id=&quot;subtitles/sq&quot; bandwidth=&quot;139&quot;&gt;
                &lt;BaseURL&gt;subtitles/sq/TDCS00187_SPY001285-00001ALB.vtt&lt;/BaseURL&gt;
              &lt;/Representation&gt;
            &lt;/AdaptationSet&gt;
          &lt;/Period&gt;
        &lt;/MPD&gt;
  </code></pre>
  <p>I want to alter the below complete node(first audio type node)</p>
  <pre><code>&lt;AdaptationSet mimeType=&quot;audio/mp4&quot; startWithSAP=&quot;1&quot; segmentAlignment=&quot;true&quot; lang=&quot;en&quot;&gt;
    &lt;SegmentTemplate timescale=&quot;1000&quot; duration=&quot;2000&quot; initialization=&quot;$RepresentationID$/init.mp4&quot; media=&quot;$RepresentationID$/seg-$Number$.m4s&quot; startNumber=&quot;1&quot;/&gt;
    &lt;Representation id=&quot;audio/en/mp4a.40.2&quot; codecs=&quot;mp4a.40.2&quot; bandwidth=&quot;131529&quot; audioSamplingRate=&quot;48000&quot;&gt;
      &lt;AudioChannelConfiguration schemeIdUri=&quot;urn:mpeg:mpegB:cicp:ChannelConfiguration&quot; value=&quot;2&quot;/&gt;
    &lt;/Representation&gt;
  &lt;/AdaptationSet&gt;
  </code></pre>
  <p>with it's any other node(audio type node)</p>
  <pre><code>&lt;AdaptationSet mimeType=&quot;audio/mp4&quot; startWithSAP=&quot;1&quot; segmentAlignment=&quot;true&quot; lang=&quot;tr&quot;&gt;
    &lt;SegmentTemplate timescale=&quot;1000&quot; duration=&quot;4800&quot; initialization=&quot;$RepresentationID$/init.mp4&quot; media=&quot;$RepresentationID$/seg-$Number$.m4s&quot; startNumber=&quot;1&quot;/&gt;
    &lt;Representation id=&quot;audio/tr/mp4a.40.2&quot; codecs=&quot;mp4a.40.2&quot; bandwidth=&quot;127472&quot; audioSamplingRate=&quot;44100&quot;&gt;
      &lt;AudioChannelConfiguration schemeIdUri=&quot;urn:mpeg:mpegB:cicp:ChannelConfiguration&quot; value=&quot;2&quot;/&gt;
    &lt;/Representation&gt;
  &lt;/AdaptationSet&gt;
  </code></pre>
  <p>And save this content into a new xml file.</p>
  <p>Is there any methods to replace nodes?</p>
  <p>I tried by searching the nodes with their 'lang' parameter, but I couldn't get it.</p>
  <p>To be clearly Any audio node has to be replaced(shifted/altered) with audio node.</p>
  <p>e.g.
  I want the node which is having the lang=&quot;bg&quot; to be shifted with the node which is having the lang=&quot;en&quot;.</p>
  <p>Any help would be appreciate. Thanks in advance.</p>