user8554358
user8554358

Reputation:

ElementTree not printing attributes

Hello I have the next xml file:

<?xml version="1.0"?>
<!-- MPD file Generated with GPAC version 0.7.2-DEV-rev986-gb3353c225-master  at 2020-04-05T02:33:59.948Z-->
<MPD xmlns="urn:mpeg:dash:schema:mpd:2011" minBufferTime="PT1.500S" type="static" mediaPresentationDuration="PT0H3M0.000S" maxSubsegmentDuration="PT0H0M7.000S" profiles="urn:mpeg:dash:profile:isoff-on-demand:2011">
 <ProgramInformation moreInformationURL="http://gpac.io">
  <Title>video.mpd generated by GPAC</Title>
 </ProgramInformation>

 <Period duration="PT0H3M0.000S">
  <AdaptationSet segmentAlignment="true" maxWidth="2592" maxHeight="1080" maxFrameRate="24" par="2592:1080" lang="eng" startWithSAP="1" subsegmentAlignment="true" subsegmentStartsWithSAP="1">
   <Representation id="1" mimeType="video/mp4" codecs="avc1.640032" width="2592" height="1080" frameRate="24" sar="1:1" bandwidth="1729262">
    <BaseURL>video-1080_dashinit.mp4</BaseURL>
    <SegmentBase indexRangeExact="true" indexRange="920-1263">
     <Initialization range="0-919"/>
    </SegmentBase>
   </Representation>
   <Representation id="2" mimeType="video/mp4" codecs="avc1.640020" width="1728" height="720" frameRate="24" sar="1:1" bandwidth="862289">
    <BaseURL>video-720_dashinit.mp4</BaseURL>
    <SegmentBase indexRangeExact="true" indexRange="919-1262">
     <Initialization range="0-918"/>
    </SegmentBase>
   </Representation>
   <Representation id="3" mimeType="video/mp4" codecs="avc1.64001E" width="864" height="360" frameRate="24" sar="1:1" bandwidth="444517">
    <BaseURL>video-360_dashinit.mp4</BaseURL>
    <SegmentBase indexRangeExact="true" indexRange="919-1262">
     <Initialization range="0-918"/>
    </SegmentBase>
   </Representation>
  </AdaptationSet>
  <AdaptationSet segmentAlignment="true" lang="eng" startWithSAP="1" subsegmentAlignment="true" subsegmentStartsWithSAP="1">
   <Representation id="4" mimeType="audio/mp4" codecs="mp4a.40.2" audioSamplingRate="44100" bandwidth="129580">
    <AudioChannelConfiguration schemeIdUri="urn:mpeg:dash:23003:3:audio_channel_configuration:2011" value="2"/>
    <BaseURL>video-audio_dashinit.mp4</BaseURL>
    <SegmentBase indexRangeExact="true" indexRange="863-1338">
     <Initialization range="0-862"/>
    </SegmentBase>
   </Representation>
  </AdaptationSet>
 </Period>
</MPD>

And I'm trying to get the value of the attribute segmentAlignment from the tag in Python like this:

tree = ET.parse(MPDPath)
root = tree.getroot()

Periods = root.findall('Period')

for Period in Periods:

    AdaptationSets = Period.findall('AdaptationSet')

    for AdaptationSet in AdaptationSets:

        print(AdaptationSet.attrib['segmentAlignment'])

But it always return None or no string is extracted from that attribute...

What I'm doing wrong? I know the MPD Path is correct.

Upvotes: 0

Views: 467

Answers (1)

Sowjanya R Bhat
Sowjanya R Bhat

Reputation: 1168

Your code should use namespace for finding in this XML like so:

ns = {'url': 'urn:mpeg:dash:schema:mpd:2011'}

for period in root.findall('url:Period', ns):
    adaptationSets = period.findall('url:AdaptationSet', ns)
    for adaptset in adaptationSets:
      print(adaptset.attrib['segmentAlignment'])

Here is the documentation : https://docs.python.org/3/library/xml.etree.elementtree.html#parsing-xml-with-namespaces

Upvotes: 1

Related Questions