XML (eXtensible Markup Language)

Overview

XML, or eXtensible Markup Language, is a widely-used markup language that defines a set of rules for encoding documents in a format that is both human-readable and machine-readable. It is characterized by its tree structure, which uses nested elements and attributes to represent data. XML is known for being more verbose and complex compared to other data interchange formats like JSON. Despite this complexity, XML remains an established standard, particularly in web services, due to its flexibility and ability to represent complex data structures.

Definition

XML serves as a more complex data format for data interchange. It is still in use today, especially in scenarios where the complexity and richness of data representation are required.

Example of XML Data

To illustrate the structure and verbosity of XML, consider the following example, which fetches the XML version of the World Bank’s population data for Chile:

result = requests.get(
"https://api.worldbank.org/v2/country/CL/indicator/SP.POP.TOTL?format=xml")
pop_data = result.text[3:]             #A
print(pop_data)

<?xml version="1.0" encoding="utf-8"?>
<wb:data page="1" pages="2" per_page="50" total="64" sourceid="2" lastupdated="2024-06-28" xmlns:wb="http://www.worldbank.org">
  <wb:data>
    <wb:indicator id="SP.POP.TOTL">Population, total</wb:indicator>
    <wb:country id="CL">Chile</wb:country>
    <wb:countryiso3code>CHL</wb:countryiso3code>
    <wb:date>2023</wb:date>
    <wb:value>19629590</wb:value>
    <wb:unit />
    <wb:obs_status />
    <wb:decimal>0</wb:decimal>
  </wb:data>
  <wb:data>
    <wb:indicator id="SP.POP.TOTL">Population, total</wb:indicator>
    <wb:country id="CL">Chile</wb:country>
    <wb:countryiso3code>CHL</wb:countryiso3code>
    <wb:date>2022</wb:date>
    <wb:value>19603733</wb:value>
    <wb:unit />
    <wb:obs_status />
    <wb:decimal>0</wb:decimal>
  </wb:data>
    …    #B
</wb:data>
#A Skip extra characters
#B Several records omitted.

This example demonstrates several key characteristics of XML:

  • Verbosity: XML is known for its verbose nature, where tags often take up more space than the data they enclose.
  • Tree Structure: The data is organized in a nested or hierarchical manner, which is typical of XML documents.
  • Metadata: XML documents often include a substantial header of metadata before the actual data begins.

On a spectrum from simple to complex data files, XML is positioned towards the complex end, with formats like CSV or delimited files being simpler. Despite its complexity, XML's ability to represent intricate data structures makes it a valuable tool in various applications, particularly in web services and data interchange scenarios.