Reading an XML file, hi, everyone.
Today we're going to talk about a very important topic, which is the XML files.
So in this section you will learn how to work with XML files, how to use and parse them by using the
element three API with Python, three point seven.
So XML in general, is standardised device type actually a structure or a file that is easily readable
from the user and is created back in 1996.
The XML document contains data structures based on content developers, which are stated by different
markers and are positioned in Iraq level.
OK, so you would have like the higher level on the outer layer and then on the inner layers you have
the lower level code that is contained within the file.
So the example is pretty much used to represent prioritized data in a standard text format.
So when we're working with XML files, we're usually going to be creating XML documents signi sending
them as bodies of HTP requests and receiving actually the XML file from the web, very similar to the
Jason files.
But this time the structure is slightly different.
So when we're using examples, there are two main approaches to that.
The first one is to read the whole document and to create the object based representation of the document
and then transferring it to the object oriented API for manipulation.
The second way is processing the document from the starting to the endpoint and performing the actions
once we see specific tasks.
For example, when we see the task, we're performing some programming action with that task, even
though the document is not done yet.
So let's so let's go to the ID and I will show you there how we can process XML files with the elementary.
So here I am in the terminal, guys, and let's import the XML tree into our devices.
So I write import xml dot e three dot element three us it actually we here to do this in python mode.
So let me actually copy it and let's go to Python Moult and then write the same so we get an error here
because it is a slight mistake.
Instead of import ETR it should be import OK.
And now we import the military as it is.
So if a right route equals etti dot element in the brackets, if I type root OK.
And so in that way we can create the previously mentioned XML document by using the elementary.
So as we can see here, by creating growth, we are starting back by creating the root element which.
Will be the actually the examiner representation of the Python file.
So if I write it, don't dump route, OK?
He's actually represented actually this representation is an XML representation of rule to root type.
OK, so here from the root python type we actually created on XML.
Now let's do another example and I'll write the book.
You quotes etty that element.
And then book, OK, and then I write route dot up and book, OK, and then t dot dump route.
OK, so as you can see now guys were created the route, which is the outer layer, and then we created
a book and we appended this book inside our route.
OK, so you created to level structure XML file, which is this hierarchical order that I was talking
about earlier on.
So let's see if you'd like to add another element we can to name.
OK, name.
And then I will make this equal to eat dot sope element and then I will write a book Colma and then
name.
OK, let's close the brackets here and once I write this, if I type after that name dot text equals
book one and then we can display what we've got already and actually created by etty dot dump route.
And here as you can see we have the outer layer route, then the inner layer will have it as a book
because you can see here and then inside we have a name and actually we wrote a value for this name,
go to book one.
OK, and we did this by writing name the text.
So what we did here is we created the name layer, as you can see here, which is a subelements type,
OK?
And we just buy it dot the payment element.
So you can see that the previous ones, there were elements, so they didn't have actual value.
But here we create the element and then we implemented it into the rule.
And this is how we've got the name book one inside.
Quite simple, isn't it?
So basically this is the way that you can create an example.
But let's actually also practice some other very nice features of the XML files.
So let me show you one thing.
So here, if I write them equals t dot soap element and then ruled comma them.
OK.
And after that, if I write it dot dump rules.
OK, so we added this in the route as you can see here.
So we have a route book and then we have a name.
And also here on the same level we have temp.
OK, so that's what we're producing in effect right now.
Rules dot remove.
You see how we can remove the temporal layer if we wouldn't like to have it.
So if I press this, you get nothing.
But if I write it dot dot rules, you can see that now our temp layer is completely removed.
So this is the way that you can remove actually elements from the XML file.
Now, if you want to display the XML file in a nice visual way, because obviously if you have a very
long XML file and trust me, I've been working with XML files long.
Ten thousand lines.
So imagine ten thousand lines of split like this.
Basically the XML representation kinda loses its advantages over the other representations.
So let me show you how can represented in quite a nice way.
So if I write import X amount dot doma dot many dumb as many dumb.
Let's print.
Mini dome dot.
Parts during and after that.
Right here in the break, it's E.T. dot, dot.
13, OK, to string and then root, so the root will be to drink not.
To pre t t, y, x.
And Mel, to pretty accent Mel, that's it.
Let's run it.
And as you can see here, the things are looking actually quite different from before.
So you can see the hair there in the version of the XML, which is a version of one, then you have
the root.
OK, then you have the inner layer, which is a subletter, the book, and then the actual value, the
player which has the value of the book, which is called Bourquin, this is the name you can obviously
have on.
There are some values like type Outr and so on.
But basically this is how one XML file looks like and this is one that you just create by yourself.
So this is quite nice.
So the next thing I will do, guys, is we're actually going to use the XML books file and I will show
you.
So let me open it for you.
So just a 4.0 Provigil.
And this is XML file with different books.
They are names and so on.
So let me copy it.
And and we can actually paste it in the same folder as your other files in the Section three.
So if you select Section three and they copied and then if we select.
Command commentary, I can simply place the books XML here, as you can see, and if you open it, you
can see actually the same file here.
So this is the book Sex Symbol, guys.
And once we have this file, I obviously attach it to the final lecture of this section.
Now we can write a Python script in order to assess this file and use the information and transfer it
to Python file.
So if I go here into python mode, so now I am in the python mode, guys.
So let's assess the file here and let's see what operations we can actually perform with it.
So the first thing I would do is import xml dot e three, ok, xml a tree dot elements three us etty
ok.
And then let's write books equals t dot files and then write books dot xml and this is the file would
just add it into our resources section three folder.
So when ready to consider the comment is accepted.
So if I write now ruled Yuko's books dot get ruled and I will break it in that way you can assess the
root node here and if I write perent root.
You can see that we get the element ruled out its location, we can also assess certain strength from
the example by identifying what type of data this is.
So if I write the print and then route that tag, we can see that the tack of the route is route.
We can also see the different attributes of each book by iterating between the books.
And this is also very simple.
So you can simply do for child, for child in route.
OK, and now we are starting the fallout, by the way, you can also do this here in the python mode,
you can print and then child.
Not that Cuomo child, that's our tribute.
OK, OK, let me just fix my print statement so we'll get some getting some inundation era when the
music, the follow up from the python mode here.
Obviously, later on we're going to create a file where I will show you how it's done.
But you can also test the attributes with the following way.
For example, FRATA ruled that atrip.
And I put in a print statement, let's say we're going to see that, for example, in The Root, um,
we don't have any attributes at the moment.
So instead of showing you all this in Python model, let's actually create the file where we're going
to be able to define all the things we need from the book's XML file.
So I will create a new file in Section three.
I will select a new file actually.
Let's select a python file and here I will type iterate books.
OK, so this is how the file will be called.
And let's write from.
Ximo dot e three dot s.
element, three import eater parts, OK, you can select that from here and let's create a function
here, so give books and in the brackets list.
Right file.
OK, and here write for event command element in Interplast.
OK, and then file in here.
Right.
If event is equal to starts and element dot tag is equal to ruled.
OK then I would like to happen is to write book dot element and then if event is equal to and and element
dot that is equal to the book we would like to print in that case.
OK, so we would like to print the zero and then one, then two, then three and four, that's it.
And here I thought format's elements dot find text and then title comma element dot fien.
OK, and actually let's place those on a different intentionally.
Also our code is more representable so let's try to here this and then element.
Yes.
Dot find text and the text.
I would like to find this publisher.
OK, so the next thing we'll find is the publisher and after this writing are common and let's write
element dot find text and here will search for number of chapters.
OK, that's it.
And after that let's write elements dot find text and search for page page count.
We're just trying to search for basically every possible element that we can find so we can practice
our search into the XML file.
So let's write in Element Dot Find.
Text and then let's right out there, OK, and let's close all the brackets, so let's create another
if statement here in the outer layer.
So I would do if event equals.
And so in the previous we did start and then we are doomed to case of pencil first with it if the event
equals to end and attack his book.
And now two, if the event is equal to end and element that attack.
Is equal to chapter.
All right, so if this is the case, we print, OK, we'll print zero and then one and then two.
OK, so let's right here, Dalts, and we're going to the format, but let's play the format on the
next the tension level in here, I'll write elements that find text and here we're going to try finding
the chapter numbers.
So write chapter number.
OK, so let's write comma here, then I will write element that find text, OK, and then I will search
for chapter title.
OK, and after that I'll write element that finds text and then let's write.
Page count.
OK.
And that's it here.
So this function can be run by writing the and calling them in class.
All right.
Name
equals.
Maine, actually, Maine, OK, in the books open and then books that ximo so our country is ready.
Guys, let's save it and let's go to the terminal and go into Python and then iterate books.
And once they do that, you can see that first we're getting one chapter one and 30 should be the page
count here and then we get chapter two in Chapter two has twenty five pages to display here this then
we get all the information for the book so we get the book.
Is Python to Out or Mark Martin.
Why then we have 13 chapters, um, 500 pages and outrace out or one and then we get obviously information
about the chapters.
So we have one chapter 30 pages and there are twenty five and so on.
This is for book two.
So this is how our function here is working.
And obviously first we get information for the book and then we'll get information for the chapter and
you can see all the pages.
And this is a way actually that you can take a simple XML file and transfer it into a python representation
quite easily.
So this XML file are all over the Internet and now we know how to work with them and transfer them into
python representation.
That's it.
Guys, thanks very much for watching.
I appreciate you spending this time to check out the XML representation in transferring to Python.
And I will see you in our next video.