Learning Python Network Programming

(Sean Pound) #1
Chapter 3

For now, we're going to focus on the object-based approach by using a Python XML
API called ElementTree. The second so-called pull or event-based approach (also
often called SAX, as SAX is one of the most popular APIs in this category) is more
complicated to set up, and is only needed for processing large XML files. We won't
need this to work with Amazon S3.


The basics of ElementTree


We'll be using the Python standard library implementation of the ElementTree API,
which is in the xml.etree.ElementTree module.


Let's see how we may create the aforementioned example XML document by using
ElementTree. Open a Python interpreter and run the following commands:





import xml.etree.ElementTree as ET








root = ET.Element('inventory')








ET.dump(root)






We start by creating the root element, that is, the outermost element of the document.
We create a root element here, and then print its string representation
to screen. The representation is an XML shortcut for </
inventory>. It's used to show an empty element, that is, an element with no data
and no child tags.


We create the element by creating a new ElementTree.Element
object. You'll notice that the argument we give to Element() is the name of the
tag that is created.


Our element is empty at the moment, so let's put something in it.
Do this:





cheese = ET.Element('cheese')








root.append(cheese)








ET.dump(root)






Now, we have an element called in our element. When an
element is directly nested inside another, then the nested element is called a child of
the outer element, and the outer element is called the parent. Similarly, elements that
are at the same level are called siblings.

Free download pdf