Wednesday, May 13, 2020

Parsing XML with namespaces in Python

Recently I had to parse an XML response from a webservice in Python for validation. I found that resources are limited when it comes to parsing XML files with namespaces in the internet. So, I thought of creating one by myself for reference.

Sample XML response:
<env:Envelope xmlns:env="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <env:Header/>
   <env:Body>
      <ser:customerServiceResponse xmlns:ser="http://xml.abc.com/customer/services">
         <ser:customerReturn>
            <part: xmlns:part="http://xml.abc.com/customer/parts">
               <part:customerPart>
                  <n2:custName xsi:nil="true" xmlns:n2="http://xml.abc.com/parts"/>
                  <n3:compName xmlns:n3="http://xml.abc.com/parts">
                     <n3:bussName>testABC</n3:bussName>
                  </n3:compName>
                  <part:customerId>962977565</part:customerId>
            </part:customerPart>
         </ser:queryCustomerReturn>
      </ser:customerServiceResponse>
   </env:Body>
</env:Envelope>

Objective:
Extract the value of customerId from above xml

Code:
import xml.etree.ElementTree as ET
import requests

def test_xml_extract():
# define body and headers

#get the response
response = requests.post(url, data=body, timeout=60, headers=headers)

#get the lement tree as root
root = ET.fromstring(response.content)

#define namespaces as a dict
ns = {'env': 'http://schemas.xmlsoap.org/soap/envelope',
      'ser': 'http://xml.abc.com/customer/services',
      'part': 'http://xml.abc.com/customer/parts'}

#extracting the element
cust_id = root.find('.//part:customerPart/part:customerId', ns).text
print("CustId : {}".format(cust_id))

In the above example we used a xpath style search and retriving the text from the element.



Sunday, April 10, 2011

April fool

"Try to be a fool for one day and then you will realize that you are a fool till that day in your life"

Well, the above sentence suits best for me. I have been an open minded generous person whole my life. That's what I thought about me. But I was not. I expected others to be the same way as I am to them, in my life. When this doesn't happen I tend to react in a negative way by hating those persons. Because of this, I was alone most of my life. After many years, if I look back, I find nothing but hatred. Now I am feeling I should have taken life as it is. Expecting others to be the way you want them is the fool-est thing. So whenever you face a situation where you friends failed you, don't tend to think in negative way. Play cool. Take time to think and react. Good friends are tough to make and easy to lose.

Sunday, March 13, 2011

Persons with weird kind of judging nature

In day today life, you will come across lots and lots of people. In one way or other, we have to interact with people of different nature. Milkman, newspaper boy, auto-driver, bus conductor, food server, co - worker and the list goes on.

Normally because of our speedy life style, we dont mind most of their characters or it 'll get lost in our activities. But there are few exceptions. We tend to remember very peculiar persons. Its because they would have made an indirect impact on life.

There is this particular person in my neighborhood. Believe me, he will look like a ordinary person but he has the capacity to make the opposite person (with whom he is talking with) angry enough to either kill or commit suicide. Its because whatever you tell, he wont directly reject but say things in a way that will make you believe that you are wrong.

Even though he is wrong, he wont accept it. According to him, his point is the ultimate truth. He will stand upon his own judging ground and judge all others. General rules and guidelines wont apply to him.

I thought of defeating him on his own ground and gathered few comrades on my side and waged an attack on him one day. We fought (battle with words. no bad words involved :-)) for nearly two hours.

At the end, we convinced him that what he doing is wrong. We almost prepared to party for our victory before he said his last words : "I know what am doing now is not wrong, but I wont do this in future".



Friday, March 11, 2011

The week gone by..

I am starting this note with sadness and disappointment. Three deaths happened this week within vicinity. One is tragic train accident and two are road accident claiming the life of my fellow batch mate. I can say these are due to carelessness and disregard for rules. Hmmm..

Thats it for today. We'll meet again tomorrow with some very interesting topic.
" Persons born with with own weird kind of judgement "

Saturday, March 5, 2011

My First Blog!!

Last month I heard from my practice manager that most of the famous entrepreneurs believe and promote blogging habit. Though I dint start this blog for that reason, indeed it does interest me. So here I am, doing this. Though I don't know what to type, I love this...!..So from onwards I am going to share my day to-day experiences here in the path of life. Lets ride!