2

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

5
  • Your "XML response" is not XML (or you pasted it only partly). In any case the XML is invalid. Commented Nov 15, 2019 at 14:00
  • 1
    @Tomalak I pasted partly. Let me edit and write valid xml Commented Nov 15, 2019 at 14:03
  • Ah, that's better. However the <EMAILID> element is empty in this XML, of course the .text is None. What do you get when you try ./FCDB_BODY/MOBNO instead? Commented Nov 15, 2019 at 14:25
  • I get the value which in this case is "03006846625". I want to check before getting <EMAILID> value if it is empty or not. How should i do it? i can't find a way to insert if else here Commented Nov 15, 2019 at 14:30
  • 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: ... Commented Nov 15, 2019 at 16:02

1 Answer 1

3

Are you using the lxml package? Because with

from lxml.etree import ElementTree, fromstring
data = """..."""  # your XML string here 
tree = ElementTree(fromstring(data))

I see you have a syntax error.

Moreover, do you want to retrieve the text of < EMAILID />? Because here it has no text at all.

First edit:

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
Sign up to request clarification or add additional context in comments.

1 Comment

sorry i am using xml.etree. Also i do want to retrieve the text of < EMAILID /> but since i am performing many requests, some time the tag is empty. So i want to add any condition here to check if there is value or not

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.