(L8)Programming with Python (Intermediate Level)
(L8)Programming with Python (Intermediate Level)
(Intermediate Level)
Lesson-8 Using Web Services
● Attribute <name>Chuck</name>
● Self Closing Tag
<phone type="intl">
</phone>
</person>
import xml.etree.ElementTree as ET
data = ''' xmlParsing.py
<person>
<name>Chuck</name>
<phone type="intl">+1 734 303 4456</phone>
<email hide="yes"/>
</person>
'''
tree = ET.fromstring(data)
print('Name:',tree.find('name').text)
print('Attr:',tree.find('email').get('hide'))
import xml.etree.ElementTree as ET
input = '''
xmlParsing2.py
<stuff>
<users>
</stuff>
'''
JavaScript Object Notation(JSON)
The full-form of JSON is JavaScript Object Notation.
It means that a script (executable) file which is made of text in a programming
language, is used to store and transfer the data.
Python supports JSON through a built-in package called json.
To use this feature, we import the json package in Python script.
The text in JSON is done through quoted-string which contains the value in
key-value mapping within { }.
It is similar to the dictionary in Python.
import json
"phone" : {
JSON represents data as nested “lists”
"type" : "intl",
and “dictionaries”
"number" : "+1 734 303 4456"
},
"email" : {
"hide" : "yes"
}}'''
info = json.loads(data)
print('Name:',info["name"])
print('Hide:',info["email"]["hide"])
import json
input = '''[
{ "id" : "001",
"x" : "2", jsonEg2.py
"name" : "Chuck"
} , info = json.loads(input)
A general rule of thumb when you're deciding between SOAP and REST for building your API:
https://mixedanalytics.com/blog/list-actually-free-open-no-auth-needed-apis/
https://jsonplaceholder.typicode.com/
For XML
https://www.w3schools.com/xml/plant_catalog.xml
https://www.hindustantimes.com/rss