DEV Community

Said Olano
Said Olano

Posted on

Understanding XML: A Practical Guide to Extensible Markup Language (2026-09-05 16:32)

Understanding XML: A Practical Guide to Extensible Markup Language

XML (Extensible Markup Language) has been a cornerstone of data exchange for over two decades. Despite the rise of JSON and other formats, XML remains deeply embedded in enterprise systems, configuration files, document standards, and countless APIs. This guide walks through what XML is, how it works, and when you should reach for it.

What Is XML?

XML is a markup language designed to store and transport data in a format that is both human-readable and machine-readable. Unlike HTML, which has a fixed set of tags for describing presentation, XML lets you define your own tags to describe the structure and meaning of data.

The "Extensible" in the name is key: there are no predefined tags. You create a vocabulary that fits your domain.

A Basic Example

Here is a simple XML document describing a book catalog:

<?xml version="1.0" encoding="UTF-8"?>
<catalog>
  <book id="bk101">
    <author>Gambardella, Matthew</author>
    <title>XML Developer's Guide</title>
    <genre>Computer</genre>
    <price>44.95</price>
    <publish_date>2000-10-01</publish_date>
  </book>
  <book id="bk102">
    <author>Ralls, Kim</author>
    <title>Midnight Rain</title>
    <genre>Fantasy</genre>
    <price>5.95</price>
    <publish_date>2000-12-16</publish_date>
  </book>
</catalog>
Enter fullscreen mode Exit fullscreen mode

Core Building Blocks

XML documents are composed of a few fundamental parts:

  • Prolog: The optional <?xml version="1.0" encoding="UTF-8"?> declaration at the top.
  • Elements: The building blocks, defined by opening and closing tags like <title>...</title>.
  • Attributes: Name-value pairs inside a tag, such as id="bk101".
  • Text content: The data between tags.
  • Root element: Every well-formed document has exactly one top-level element (here, <catalog>).

Well-Formed vs. Valid

XML defines two important levels of correctness.

Well-formed means the document follows XML syntax rules:

  • Exactly one root element
  • Every opening tag has a matching closing tag
  • Tags are properly nested (no overlapping)
  • Attribute values are quoted
  • Special characters are escaped

Valid means the document is well-formed and conforms to a defined structure, expressed via a schema.

Escaping Special Characters

Five characters have special meaning and must be escaped in text:

Character Entity
< &lt;
> &gt;
& &amp;
" &quot;
' &apos;

For larger blocks of unescaped content, use a CDATA section:

<script><![CDATA[
  if (a < b && b > c) { doSomething(); }
]]></script>
Enter fullscreen mode Exit fullscreen mode

Defining Structure with Schemas

To enforce rules about which elements and attributes are allowed, XML supports schema languages. The two most common are DTD and XSD.

XML Schema Definition (XSD)

XSD is itself written in XML and supports data types, making it the modern standard:

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">
  <xs:element name="book">
    <xs:complexType>
      <xs:sequence>
        <xs:element name="author" type="xs:string"/>
        <xs:element name="title" type="xs:string"/>
        <xs:element name="price" type="xs:decimal"/>
      </xs:sequence>
      <xs:attribute name="id" type="xs:string" use="required"/>
    </xs:complexType>
  </xs:element>
</xs:schema>
Enter fullscreen mode Exit fullscreen mode

Namespaces

Namespaces prevent naming collisions when combining XML from different vocabularies. They are declared with the xmlns attribute and typically use a URI as a unique identifier:

<root xmlns:h="http://www.w3.org/TR/html4/"
      xmlns:f="https://example.com/furniture">
  <h:table>
    <h:tr><h:td>Apples</h:td></h:tr>
  </h:table>
  <f:table>
    <f:name>Coffee Table</f:name>
    <f:width>80</f:width>
  </f:table>
</root>
Enter fullscreen mode Exit fullscreen mode

Here both <table> elements coexist without conflict because they belong to different namespaces.

Querying and Transforming XML

Two related standards make XML powerful for processing:

  • XPath: A query language for navigating and selecting nodes. For example, /catalog/book[@id='bk101']/title selects a specific title.
  • XSLT: A language for transforming XML into other formats such as HTML, plain text, or a different XML structure.

A short XPath example in context:

//book[price > 40]/title
Enter fullscreen mode Exit fullscreen mode

This selects the titles of all books costing more than 40.

Parsing XML in Code

Most languages ship with XML libraries. Here is an example using Python's built-in ElementTree:

import xml.etree.ElementTree as ET

tree = ET.parse("catalog.xml")
root = tree.getroot()

for book in root.findall("book"):
    title = book.find("title").text
    price = book.find("price").text
    print(f"{title}: ${price}")
Enter fullscreen mode Exit fullscreen mode

There are two general parsing strategies to be aware of:

  • DOM parsing loads the entire document into memory as a tree. Convenient but memory-heavy.
  • **SAX

Top comments (0)