简体   繁体   中英

I am having a IndexError: list index out of range error trying to grab the text from an xml file

I am having an error when trying to iterate through the metadata tags from my xml file and grabbing the text from each tag. The blanks text makes the number of tags smaller than it actually is. For example: Name="som" the error hits there. How can I resolve this?

xmldoc = minidom.parse('manifest-initial.xml')
node = xmldoc.documentElement

itemlist = xmldoc.getElementsByTagName('MetaData')
for i in range(0, len(itemlist)): 
    if xmldoc.getElementsByTagName('MetaData')[i].childNodes[0].nodeValue is None:
       print("None")
    print(xmldoc.getElementsByTagName('MetaData')[i].childNodes[0].nodeValue)



<MetaData Name="scrid">5211780</MetaData>
                        <MetaData Name="Abbeviation">HT</MetaData>
                        <MetaData Name="Partner Due Date">02/21/2019</MetaData>
                        <MetaData Name="version">1</MetaData>
                        <MetaData Name="asset">episode</MetaData>
                        <MetaData Name="psa.orig.providerAssetId">80</MetaData>
                        <MetaData Name="Publish Date">20.0</MetaData>
                        <MetaData Name="activationDate">26</MetaData>
                        <MetaData Name="som"></MetaData>
                        <MetaData Name="cadq"></MetaData>
                        <MetaData Name="cadr"></MetaData>

Output from terminal stops at "som".

There is no child element for your code to evaluate - thus being out of range. Check to see if the child element exists instead of seeing what value it contains.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM