如何检查 xml 标签值是否为 python 中的 none
How to check if xml tag value is none in python
我正在尝试比较两个 api 的响应数据。其中之一 returns xml。问题是 xml 响应中的标签之一为空。我找不到检查该值是否存在的方法。
这里是 xml 回复
<?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>
请注意 return None 值。
这里是需要修改的代码。提前致谢。
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
您使用的是 lxml 包吗?因为
from lxml.etree import ElementTree, fromstring
data = """...""" # your XML string here
tree = ElementTree(fromstring(data))
我看到你有语法错误。
此外,您是否要检索 的文本?因为这里根本没有文字。
第一次编辑:
from xml.etree import ElementTree
data = """...""" # Your XML string
tree = ElementTree.ElementTree(ElementTree.fromstring(data))
root = tree.getroot()
for l in root.findall("./FCDB_BODY/EMAILID"):
if l is not None:
xml_email = l.text
if xml_email:
print("XML Email is", xml_email)
else:
print("There is no email for this entry")
else:
pass
我正在尝试比较两个 api 的响应数据。其中之一 returns xml。问题是 xml 响应中的标签之一为空。我找不到检查该值是否存在的方法。
这里是 xml 回复
<?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>
请注意 return None 值。
这里是需要修改的代码。提前致谢。
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
您使用的是 lxml 包吗?因为
from lxml.etree import ElementTree, fromstring
data = """...""" # your XML string here
tree = ElementTree(fromstring(data))
我看到你有语法错误。
此外,您是否要检索
第一次编辑:
from xml.etree import ElementTree
data = """...""" # Your XML string
tree = ElementTree.ElementTree(ElementTree.fromstring(data))
root = tree.getroot()
for l in root.findall("./FCDB_BODY/EMAILID"):
if l is not None:
xml_email = l.text
if xml_email:
print("XML Email is", xml_email)
else:
print("There is no email for this entry")
else:
pass