Learning Python Network Programming

(Sean Pound) #1

APIs in Action


Getting started with XML


The Extensible Markup Language (XML) is a way of representing hierarchical
data in a standard text format. When working with XML-based web APIs, we'll be
creating XML documents and sending them as the bodies of HTTP requests and
receiving XML documents as the bodies of responses.


Here's the text representation of an XML document, perhaps this represents the stock
at a cheese shop:


<?xml version='1.0'?>
<inventory>
<cheese id="c01">
<name>Caerphilly</name>
<stock>0</stock>
</cheese>
<cheese id="c02">
<name>Illchester</name>
<stock>0</stock>
</cheese>
</inventory>

If you've coded with HTML before, then this may look familiar. XML is a markup
based format. It is from the same family of languages as HTML. The data is
structured in an hierarchy formed by elements. Each element is represented by two
tags, a start tag, for example, , and a matching end tag, for example, .
Between these two tags, we can either put data, such as Caerphilly, or add more
tags, which represent child elements.


Unlike HTML, XML is designed such that we can define our own tags and create our
own data formats. Also, unlike HTML, the XML syntax is always strictly enforced.
Whereas in HTML small mistakes, such as tags being closed in the wrong order,
closing tags missing altogether, or attribute values missing quotes are tolerated,
in XML, these mistakes will result in completely unreadable XML documents.
A correctly formatted XML document is called well formed.


The XML APIs


There are two main approaches to working with XML data:



  • Reading in a whole document and creating an object-based representation of
    it, then manipulating it by using an object-oriented API

  • Processing the document from start to end, and performing actions as
    specific tags are encountered

Free download pdf