TExcelTAD
Home › XML › XML How to Use

XML How to Use

XML's job is to move and store structured information in a way that both people and programs can read. This lesson looks at the typical ways XML is put to work, and why the format became so popular for those jobs.

1. Separate data from presentation

A web page that hard-codes a product list in HTML has to be edited every time a price changes. If the products live in an XML file instead, the page can load the file and build the HTML from it. The designer changes the layout, the shop manager changes the data, and neither touches the other's file.

html · try it yourself
<!DOCTYPE html>
<html>
<body>
  <h3>Today's specials</h3>
  <ul id="list"></ul>
  <script>
    // In a real site this text would come from a file such as specials.xml
    const xmlText = `<specials>
      <dish price="7.50">Tomato soup</dish>
      <dish price="12.00">Mushroom risotto</dish>
      <dish price="5.25">Apple crumble</dish>
    </specials>`;

    const doc = new DOMParser().parseFromString(xmlText, 'application/xml');
    for (const dish of doc.querySelectorAll('dish')) {
      const li = document.createElement('li');
      li.textContent = `${dish.textContent} — €${dish.getAttribute('price')}`;
      document.getElementById('list').append(li);
    }
    console.log(`Built ${doc.querySelectorAll('dish').length} list items from XML`);
  </script>
</body>
</html>

Change a price or add a <dish> in the XML string and run it again — the HTML follows the data.

2. Exchange data between systems

Two programs written in different languages, on different operating systems, can still agree on an XML format. Plain text travels through email, HTTP, message queues and file shares without special handling, and every major language ships an XML parser.

Situation Typical XML format
Bank payments ISO 20022 (pain.001, camt.053 …)
News and podcasts RSS 2.0, Atom
Electronic invoices UBL, Factur-X
Search engine hints Sitemaps (sitemap.xml)
Vector graphics SVG
Word processing Office Open XML, OpenDocument

3. Store configuration and documents

Many tools keep their settings in XML because it handles nesting naturally and can be validated against a schema:

xml · try it yourself
<?xml version="1.0" encoding="UTF-8"?>
<appConfig version="2">
  <database host="localhost" port="5432">
    <name>inventory</name>
    <poolSize>10</poolSize>
  </database>
  <logging level="warn">
    <file>/var/log/inventory.log</file>
  </logging>
</appConfig>

4. Make data outlive its software

Proprietary binary formats can become unreadable when the program that wrote them disappears. XML is plain Unicode text with descriptive names, so archives, libraries and governments use it for documents that must remain readable for decades.

5. Build new languages

Because XML lets you define your own vocabulary, whole industries have built languages on it: XHTML, SVG, MathML, RSS, KML (maps), MusicXML (sheet music), GPX (GPS tracks) and many more. Each one is "just XML" with an agreed set of element names, so the same tools — parsers, XPath, XSLT, schema validators — work on all of them.

xml · try it yourself
<gpx version="1.1" creator="ExcelTAD">
  <trk>
    <name>Morning run</name>
    <trkseg>
      <trkpt lat="48.8566" lon="2.3522"><ele>35</ele></trkpt>
      <trkpt lat="48.8570" lon="2.3530"><ele>36</ele></trkpt>
    </trkseg>
  </trk>
</gpx>

Note: A real GPX file also declares a namespace (xmlns="http://www.topografix.com/GPX/1/1"). You'll learn what that means in the Namespaces lesson.

When not to use XML

XML is verbose. For small messages between a browser and your own API, JSON is usually shorter and easier to work with in JavaScript. XML shines when documents mix text and markup, when you need namespaces, strong schema validation, or when an industry standard already requires it. The XML vs JSON lesson compares them in detail.

Test yourself

Why is keeping data in XML separate from HTML useful?

  1. XML loads faster than HTML
  2. The data can change without editing the page layout
  3. Browsers can't display HTML lists
  4. XML encrypts the data

Which of these is an XML-based language?

  1. CSS
  2. SVG
  3. Python
  4. Markdown