From the course: Python: XML, JSON, and the Web

Unlock the full course today

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

The ElementTree API

The ElementTree API - Python Tutorial

From the course: Python: XML, JSON, and the Web

Start my 1-month free trial

The ElementTree API

- [Instructor] While the DOM API attempts to create a fully functional cross-language and cross-platform API for working with XML data, the ElementTree API takes a different approach. It focuses instead on being a simpler and more efficient way of working with XML and this leads to some important differences from the DOM. First, elements are treated as if they were lists. What this means is that if you have an XML element that contains other elements, you can iterate over those child elements using standard iteration, like a foreloop. Next, attributes are treated like dictionaries. So if you have a reference to an element, then you can access its attrib property which is a dictionary of all the attribute names and values. ElementTree also makes searching for content within XML very straightforward to do. The API provides defined and find all functions which can use XPath Syntax to search the XML for specific data. Now again, I'm not going to go deep into XPath here. If you want the…

Contents