XML Tutorial · XML Basics · Lesson 1 of 28

XML Introduction

What XML is, what it's used for today, how it differs from HTML and JSON, and how to read and write it in Python, PHP and Java.

Create a free account to save this page, take notes and track your progress. Sign in

What is XML?

XML (eXtensible Markup Language) is a text format for structured data. You invent the tags that describe your data, nest them into a tree, and any XML parser in any language can read the result:

<?xml version="1.0" encoding="UTF-8"?>
<order id="1042">
  <customer>Ada Lovelace</customer>
  <item sku="TEA-01" qty="2">Green tea</item>
  <item sku="MUG-07" qty="1">Camel mug</item>
</order>

XML became a W3C standard in 1998 and is still everywhere, often out of sight:

  • Documents: Word (.docx), Excel (.xlsx) and PowerPoint files are zipped XML; so are EPUB books and SVG images.
  • Configuration: Maven pom.xml, Android manifests, .NET project files, Spring and many enterprise tools.
  • Integration: SOAP web services, banking (ISO 20022 payment messages), healthcare (HL7 CDA), e-invoicing (UBL), government and publishing (DocBook, DITA, JATS).
  • The web: RSS and Atom feeds, sitemaps (sitemap.xml) and XHTML.

XML vs HTML vs JSON

XML HTML JSON
Purpose Describe any data or document Display web pages Exchange data between programs
Tags You define them Fixed set (<p>, <div>) No tags: objects and arrays
Strictness Must be well-formed or parsing fails Browsers repair mistakes Must be valid JSON
Schemas DTD, XSD, RELAX NG, Schematron No JSON Schema
Query and transform XPath, XSLT, XQuery CSS selectors JSONPath, jq
Mixed content (text with markup) Yes Yes Awkward

Reading XML in code

The same task in three languages: parse the order above and list its items. Python uses lxml, PHP its built-in SimpleXML, Java the DOM parser that ships with the JDK. Switch tabs to compare; Python and PHP run in your browser.

Read an orderTry it yourself
from lxml import etree

xml = b"""<order id="1042">
  <customer>Ada Lovelace</customer>
  <item sku="TEA-01" qty="2">Green tea</item>
  <item sku="MUG-07" qty="1">Camel mug</item>
</order>"""

order = etree.fromstring(xml)
print("Order", order.get("id"), "for", order.findtext("customer"))
for item in order.findall("item"):
    print(f"- {item.get('qty')} x {item.text} ({item.get('sku')})")

Output

Order 1042 for Ada Lovelace
- 2 x Green tea (TEA-01)
- 1 x Camel mug (MUG-07)

JavaScript in the browser has XML built in too (DOMParser); the XML in JavaScript page has live examples.

What this tutorial covers

  1. Basics: syntax, elements and attributes, well-formedness, special characters, namespaces.
  2. Validation: DTDs, XML Schema (XSD), RELAX NG and Schematron.
  3. Querying and transforming: XPath, XSLT and XQuery.
  4. XML in code: Python, PHP, Java and JavaScript; DOM vs streaming; security (XXE).
  5. XML in the real world: SOAP, feeds and sitemaps, SVG and Office files, and when to choose XML, JSON or YAML.

Test yourself

Which of these is a key difference between XML and HTML?

  1. In XML you define your own tags
  2. XML can only store numbers
  3. HTML must be well-formed
  4. XML has no attributes
Show answer

Answer: In XML you define your own tags

HTML has a fixed vocabulary for web pages; XML is a meta-language for defining your own.

Was this page helpful?