From the course: Data Ingestion with Python

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Working in XML

Working in XML - Python Tutorial

From the course: Data Ingestion with Python

Start my 1-month free trial

Working in XML

- XML is an old and well-established protocol. It has a bad reputation, mainly since it was abused in many systems. It's still out there, and you'll probably encounter it at one point or another. There are two ways of reading XML. One is loading everything into memory, called DOM, Document Object Model. And one iterative, called SAX, which stands for Simple API for XML. The two main libraries for working with XML are the built-in ElementTree and the third-party lxml. We're going to use ElementTree since it's in the standard library. Let's have a look at the code reading this file. We import ElementTree as xml And since everything inside XML is text, we need to convert manually everything. So in line 9-17, we define the conversions. In line 20, we have iter_rides. We open the file, and in line 22 we load the whole file into memory and pass it. Then we get the root of the tree, and go for every element, we take the type, find the right converter, and convert it. In line 30, we're using…

Contents