JSON maps almost perfectly onto Python dictionaries and arrays.
Parsing XML is a lot more complex, so there are
several options to choose from. We'll look at some of
the most common. I also recommend that you have a
look at the Python XML page to see what your
options are. Let's first look at parsing XML into a
document tree. The fundamental difference between this and event driven
or, what we might call sax parsing of XML is
that, here, we're going to read the entire XML tree
into memory. We're going to use the XML e tree
Element tree module, which is part of the Python standard
library. With element tree we can parse data in a couple
of different ways. We can parse it by file name.
We can also use it from a string method of
the element tree module to parse XML stored as a
string value in our program. As we think about parsing XML,
using element trees. It makes most sense to work
with documented oriented XML data. So let's return to the
research article example. This data set from Bio med
central is both an excellent example of XML used to
encode documents and one type of data that data
scientist, specifically text minded folks, are likely to use in
their work. In extracting the data we need from
a tree, we're mostly going to be working with element objects.
Elements permit us to iterate over their children.
For example here, we've parsed our research article into
a tree and then from this tree, we're getting
the root element. Then we're going to iterate over
the children of that root element, and in
this case use the tag attribute to print out
the tag name. Of each child element. So let's
see what this looks like when we run it.
Okay, now let's take a look at that xml document
again to see where these values come from. Okay, and
here we can see that this is the root element,
and we are getting out all of the elements that are
nested immediately within that element or the direct children of
that element, ui, ji, and fm. And we can see
that those are coming out here. Now, if we want
to extract the title of an article we can see that
its found in the bibliography section of the front
matter section. Let's see how me might extract that. Element
tree supports basic x path expressions. Because, in data
wrangling we're often pulling most of the data out of
an XML document, this level of support is going
to be sufficient for many if not most of our
needs. Returning to the problem of extracting the title, we
can do that as follows. So, here, you can see
that I'm using the find method on the root
element. And I'm using an x path expression to
specify where I expect to find a title element.
That simply means, start at the current element. And
then work your way down into the fm element
and it's child the bibl or bibliography element and
then finally into the title element itself. Note that
this is very much like the way that you specify
a path in a URL or in many file systems.
Now it turns out that in this data, many of
the text elements are actually wrapped in paragraph tags. And
we can see that that's the case actually for the
title here. So what I'm going to do here is with
the title element that I got by using this xpath
expression in this find method, I'm simply going to iterate
over all of the children of title. Very similar syntax
to what we saw before when iterating over all of the
children of root. And for each child, I'm simply going to take
the text of that child and concatenate it on to my
title text. Now, so far for elements we've seen two different attributes.
The first one was tag and now we're seeing the text
attribute. There are a number of attributes for elements that are useful.
These are two of the most common that you'll end up using.
Okay, and then here we're simply going to print out the title.
So, let's go ahead and run this and then we'll
return and take a look at what this piece of code
is doing for us. Okay, and here we can see that
the title is actually selected and printed out properly. Here we're
actually printing out the author email addresses. Let's take a look
at how we did that in the code. Here we're going
to use the find all method. Instead of the Find method.
Find All with return all of the elements that match this
x path expression. And so, we're simply going to iterate over
each one of those elements and then for each one of
those elements, we're going to do a Find in order to
locate the email tag. So, let's go again then and take
a look at the data. Okay, so my x path expression
gets me this far, down here into the author group. And
then for each one of these authors, I'm doing a find
for an email element, and I'm selecting the text of that
element, here, and simply printing it out.
So again, this selects all of the individual
authors found in the author group that's part
of the bibliography section in the front matter.