I am trying to compare data of two api's responses. One of them returns xml. The issue is one of the tag in xml response is null. I can't find the way to check if the value exist or not.
Here is xml response
<?xml version="1.0" encoding="UTF-8"?>
<FCDB_RES_ENV>
<FCDB_HEADER>
<SOURCE>FCAT</SOURCE>
<FCDBCOMP>FCDB</FCDBCOMP>
<MSGID>MB_31051021591539014</MSGID>
<CORRELID>MB_31051021591539014</CORRELID>
<USERID>615</USERID>
<BRANCH>000</BRANCH>
<MODULEID>LGN</MODULEID>
<SERVICE>GetEmailMobNoDetails</SERVICE>
<OPERATION>GetEmailMobNoDetails</OPERATION>
<SOURCE_USERID>FCAT</SOURCE_USERID>
<DESTINATION>FCDB</DESTINATION>
<COUNTRYCODE>T001</COUNTRYCODE>
<USERTYPE>ENS</USERTYPE>
<LANGID>eng</LANGID>
<CHANNELID>01</CHANNELID>
</FCDB_HEADER>
<FCDB_BODY>
<EMAILID/>
<MOBNO>03006846625</MOBNO>
<FCDB_ERROR_RESP>
<ERROR>
<ECODE>00</ECODE>
<EDESC>Your transaction has been processed successfully.</EDESC>
</ERROR>
</FCDB_ERROR_RESP>
<FCDB_WARNING_RESP>
<WARNING>
<WCODE/>
<WDESC/>
</WARNING>
</FCDB_WARNING_RESP>
</FCDB_BODY>
</FCDB_RES_ENV>
Please note the return None value.
Here is the code which need to modify. Thanks in advance.
response = requests.request("POST", url, data=payload.format(cust_no, cust_type), headers=headers)
tree = ElementTree.ElementTree(ElementTree.fromstring(response2.text))
root = tree.getroot()
for l in root.findall("./FCDB_BODY/EMAILID"):
if l is not None:
xml_email = l.text
print("XML Email is", xml_email)
else:
pass
<EMAILID>element is empty in this XML, of course the.textisNone. What do you get when you try./FCDB_BODY/MOBNOinstead?emailid = root.find("./FCDB_BODY/EMAILID")(not.findall()- you only expect one hit anyway) and then:if emailid is None or emailid.text is None or emailid.text == "": ... else: ...