简体   繁体   中英

python xml ElementTree exception for tag not found

I'm using ElementTree to successfully pull values from specific tags in an xml file. To add some level of protection I'd like to throw and exception if a required tag is not present.

I found that ElementTree returns a value of "None" in place of a missing tag. But I can't even get that to work to my advantage :(

value = xmlTree.findtext('tag')
print value

Will print 'None" if the tag "tag" isn't present.

So I tried

if value == "None":
    print "tag not present"

But for some reason it won't catch it???

My next thought was to see if ElementTree had a build in exception that would detect a missing tag, but had no luck with that either.

"None" (字符串)None (表示值缺失的常量)不同

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