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 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




