TExcelTAD
Home › XML › XML Examples

XML Examples

The best way to get a feel for XML design is to read plenty of it. This lesson collects small, realistic documents of different kinds. Run each one, then try editing it — add an element, break a rule, and see how the parser reacts.

A simple record

A contact card: flat, one level of children, all text.

xml · try it yourself
<?xml version="1.0" encoding="UTF-8"?>
<contact>
  <name>Sofia Rossi</name>
  <phone type="mobile">+39 333 123 4567</phone>
  <email>sofia.rossi@example.com</email>
  <city>Bologna</city>
</contact>

A list of records

A collection element wrapping repeated items is the most common XML shape of all — think of it as a table where each <employee> is a row.

xml · try it yourself
<employees department="Support">
  <employee id="e01" since="2019">
    <name>Kwame Mensah</name>
    <role>Team lead</role>
  </employee>
  <employee id="e02" since="2022">
    <name>Hana Sato</name>
    <role>Agent</role>
  </employee>
  <employee id="e03" since="2024">
    <name>Diego Herrera</name>
    <role>Agent</role>
  </employee>
</employees>

Nested data

An invoice shows deeper nesting: a header, a customer block, line items and totals.

xml · try it yourself
<?xml version="1.0" encoding="UTF-8"?>
<invoice number="INV-2026-0187" currency="EUR">
  <issued>2026-04-30</issued>
  <seller>
    <name>Blue Kettle Tea Co.</name>
    <vatId>DE123456789</vatId>
  </seller>
  <buyer>
    <name>Café Morgenrot</name>
    <address>
      <street>Lindenstraße 4</street>
      <postcode>10969</postcode>
      <city>Berlin</city>
    </address>
  </buyer>
  <lines>
    <line no="1"><product>Assam, 1 kg</product><qty>3</qty><unitPrice>18.00</unitPrice></line>
    <line no="2"><product>Earl Grey, 1 kg</product><qty>2</qty><unitPrice>21.50</unitPrice></line>
  </lines>
  <totals net="97.00" vat="18.43" gross="115.43"/>
</invoice>

Tip: Notice that totals are stored as attributes on an empty element. That is a design choice: they are short, single values. Keeping them as child elements would be equally valid.

A document with mixed content

Books, articles and help pages mix text with inline markup. Order and white space matter here.

xml · try it yourself
<article lang="en">
  <title>Why the sky is blue</title>
  <para>Sunlight contains <term>all colours</term> of the visible spectrum.</para>
  <para>Air molecules scatter <em>short</em> wavelengths far more than long ones,
    an effect called <term ref="rayleigh">Rayleigh scattering</term>.</para>
  <para>That is also why sunsets look <em>red</em>: the light has travelled further
    through the air, and most of the blue has been scattered away.</para>
</article>

Configuration

A config file uses attributes heavily, because most settings are short values:

xml · try it yourself
<server name="api-gateway">
  <listen host="0.0.0.0" port="8080" tls="true"/>
  <limits requestsPerMinute="600" maxBodyKb="512"/>
  <routes>
    <route path="/users" target="http://users:3000"/>
    <route path="/orders" target="http://orders:3001" auth="required"/>
  </routes>
</server>

Reading an example with JavaScript

Here the invoice from above is checked: do the line totals add up to the stated net amount?

html · try it yourself
<!DOCTYPE html>
<html>
<body>
  <script>
    const xml = `<invoice number="INV-2026-0187" currency="EUR">
      <lines>
        <line no="1"><product>Assam, 1 kg</product><qty>3</qty><unitPrice>18.00</unitPrice></line>
        <line no="2"><product>Earl Grey, 1 kg</product><qty>2</qty><unitPrice>21.50</unitPrice></line>
      </lines>
      <totals net="97.00" vat="18.43" gross="115.43"/>
    </invoice>`;
    const doc = new DOMParser().parseFromString(xml, 'application/xml');
    const num = (el, sel) => Number(el.querySelector(sel).textContent);

    let sum = 0;
    for (const line of doc.querySelectorAll('line')) {
      const total = num(line, 'qty') * num(line, 'unitPrice');
      console.log(`Line ${line.getAttribute('no')}: ${line.querySelector('product').textContent} = ${total.toFixed(2)}`);
      sum += total;
    }
    const stated = Number(doc.querySelector('totals').getAttribute('net'));
    console.log(`Computed net ${sum.toFixed(2)}, stated ${stated.toFixed(2)} →`, sum === stated ? 'OK' : 'MISMATCH');
  </script>
</body>
</html>

Design patterns to notice

Pattern Seen in Why
Plural wrapper + singular items <employees>/<employee>, <lines>/<line> Easy to loop over; leaves room for siblings
id attributes employees, lines Lets other data refer to an item
Units/currency as attributes currency="EUR" Metadata about the values
Dates in ISO format 2026-04-30 Sortable, unambiguous, matches xs:date
Grouping elements <address>, <seller> Keeps related fields together

Test yourself

Why is the date written as 2026-04-30 rather than 30/04/2026?

  1. XML forbids slashes
  2. ISO 8601 dates are unambiguous, sortable and match xs:date
  3. It is shorter
  4. Browsers require it

What is 'mixed content'?

  1. Elements and attributes in one document
  2. An element containing both text and child elements
  3. Two namespaces in one file
  4. XML containing JSON