How can I determine if findAll("<specific tag name>") didn't match a tag in XML file?

16 views
Skip to first unread message

許家榮

unread,
May 5, 2016, 11:58:34 AM5/5/16
to beautifulsoup
Hi Experts,

sorry for a question,

My project requires uses specific keys to be tag name, and uses this tag name to search same tag name in a XML file.

for example:

if my XML only contains these data:

<centos>
        <name>centos</name>
        <version>7</version>
</centos>

and when I uses findAll("centos") method, I can get tag = name, attrib = centos ... etc

but, I need determine if my target tag didn't exist in my xml file, if I am uses findAll("centos"), if I uses findAll("fedora"), this script seems interrupt at for statement:

for system in soup.findAll('fedora'):

I understand if findAll couldn't find a target tag, then return empty string, I trying to uses:

if element == "":
   print "Script couldn't find target tag in this file" 

but the script seems stuck in for system in soup.findAll('xxxx').

can anyone provide a solution, thanks!
Reply all
Reply to author
Forward
0 new messages