liveBook · Manning

Xml

As explained in The Quick Python Book, Fourth Edition

XML is a text-based markup and data-interchange format that represents structured information using nested elements and attributes.

XML is a plain-text format for structured data. Angle-bracket tags nest into a tree, while attributes and element text store values and metadata. Its flexibility supports complex API responses, but its verbosity often makes extraction harder than working with simpler formats. section 22.3 section 22.3.2

When reading XML, inspect its structure before extracting values:

  • Expect a metadata-heavy header before the main records. section 22.3.2
  • Follow nested elements as a tree rather than treating the document as flat text. section 22.3.2
  • Check both element text and attributes; either may contain the needed value. section 22.3.2
  • Choose a parser or conversion library according to the extraction task; standard-library XML tools exist, but simple extraction may be inconvenient. section 19.1.4 section 22.3.2

Turning a World Bank XML response into mappings

Fetch the response with requests, remove the leading characters shown in the example, and parse it with xmltodict: population = xmltodict.parse(pop_data). The resulting mapping represents attributes with keys such as "@page" and element text with "#text"; repeated wb:data elements become a list containing Chile’s population records.

Table 19.6 Modules supporting internet protocols and formats (view table figure)

Module

Description

socket, ssl

Low-level networking interface and SSL wrapper for socket objects

Email

Email and MIME handling package

Json

JSON encoder and decoder

Mailbox

Manipulate mailboxes in various formats

Mimetypes

Map filenames to MIME types

base64, binhex, binascii, quopri, uu

Encode/decode files or streams with various encodings

html.parser, html.entities

Parse HTML and XHTML

xml.parsers.expat, xml.dom, xml.sax, xml.etree.ElementTree

Various parsers and tools for XML

cgi, cgitb

Common Gateway Interface support

Wsgiref

WSGI utilities and reference implementation

urllib.request, urllib.parse

Open and parse URLs

ftplib, poplib, imaplib, nntplib, smtplib, telnetlib

Clients for various internet protocols

Socketserver

Framework for network servers

http.server

HTTP servers

xmlrpc.client, xmlrpc.server

XML-RPC client and server

Compare Python’s XML parsers and related internet-format tools, including xml.parsers.expat, xml.dom, xml.sax, and xml.etree.ElementTree. section 19.1.4

How The Quick Python Book, Fourth Edition covers it

How deep it goes
Explains nesting, attributes, metadata, and practical parsing, but stops short of detailed XML parser internals.
Examples
Demonstrates a World Bank Chile population response, requests retrieval, and xmltodict conversion into nested mappings.
What it assumes
Assumes general programming knowledge and teaches XML structure and practical parsing without beginning with markup fundamentals.
What you can do after
Apply it via requests and xmltodict to retrieve XML API data and extract values from nested records.
How it connects
Connects XML with JSON, REST APIs, SOAP APIs, HTTP, and Python XML parsers.

Also explained in

Compare how 5 books explain xml →

Related concepts

This explainer was generated by Manning's knowledge graph from the text of The Quick Python Book, Fourth Edition. The section links point to the parts of the book it draws on.