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


Talk Python's Mastodon Michael Kennedy's Mastodon