Consuming HTTP Services in Python Transcripts
Chapter: XML services with requests
Lecture: Concept: Working with XML from Python
Login or
purchase this course
to watch this video and the rest of the course contents.
0:02
Let's review working with xml in Python.
0:05
We're not yet to the services part, but that's actually the easy part,
0:08
it's working with xml itself that can sometimes be tricky;
0:12
so here we've got a little fragment of the data that came
0:15
from that Reed college course catalogue that was reed.xml,
0:18
you can see we have a title, we have days, we have building, and room,
0:21
and if we want to work with this, we go and import ElementTree,
0:25
and just to make our lives easier, it wasn't technically necessary
0:28
but it is better to create a named tuple
0:31
which is more or less a class with fields only and no functions,
0:35
which was all we really needed it was little data containers,
0:38
and then we are going to parse that from the string representation of xml
0:43
into the DOM, the object model,
0:46
we are going to use the from string method on the ElementTree,
0:49
and then we start writing xpath, queries against it,
0:52
so here we are looping over the find all of the courses, and for each course,
0:57
or each really node that we find in the xml document, we are going to run
1:01
a set of subqueries on that document, so we are going to query for the title,
1:04
for the place/building, place/room,
1:07
and then in this example we're also getting the days that it runs.
1:10
Then if we want the courses that are held in Eliot hall, or a building,
1:13
all we have to do is write a little list comprehension
1:16
say give me the course for course in courses,
1:19
these are the transformed named tuple types, where the building=eliot.
1:23
Boom, it's done, it's worth considering the trade-off of xml versus json
1:29
and how it relates back to Soap services.
1:33
So Soap is a much more strict and honestly convoluted protocol
1:38
than simple xml exchange.
1:41
So Soap has envelops and headers and a bunch of namespaces,
1:45
and certain ways in which things must be structured,
1:48
and xml is just much more general, there can be simple xml or complicated xml.
1:52
So what we are doing and what we are going to do, for this section,
1:55
is all about just working with plain xml data, not Soap services,
2:00
Soap services require tons of tooling and we'll work on that later, right,
2:06
we'll see that Python actually has some decent answers to the Soap story
2:09
if you must use those services,
2:11
but in this case, we are just working with straight xml.
2:14
So, we are just focused on working with xml by itself, now, if you get the choice,
2:18
if you get to decide whether you have an xml service or a Json service,
2:22
choose, Json, it much more closely matches the way data is represented in Python
2:28
and the way it's represented in Javascript,
2:31
so if you are building a web app and it's got a Javascript front end
2:34
and a Python back end, Json all the way,
2:37
but, consuming xml is still not too bad as we've seen.
2:39
So, let's go look at another example.