XML Essentials
Introduction to XML
What is XML?
Think of a recipe. It has a title, an ingredients list, and a set of instructions. Each part has a clear label and its own content. This structure makes the recipe easy for anyone to follow. XML, which stands for Extensible Markup Language, does something similar for data.
XML was designed to store and transport data in a way that is both human-readable and machine-readable. Its main job isn't to display information, but to describe it. It wraps data in tags that explain what the data is.
The key idea behind XML is to separate the data from its presentation. The XML file just holds the structured information, while another program decides how to show it.
XML vs HTML
People often mix up XML and HTML. They look similar because they both use tags (words inside angle brackets like <tag>), but their goals are completely different.
HTML (HyperText Markup Language) is used to display data on a webpage. Its tags are predefined and tell a browser how to format content. For example, <h1> means "make this a top-level heading," and <p> means "this is a paragraph."
XML, on the other hand, is for describing the structure of data. You invent your own tags to fit the information you're storing. Let's say you want to store a simple note. In HTML, you might write it like this:
<h1>Reminder</h1>
<p>Don't forget to buy milk!</p>
A browser knows exactly how to display that. But in XML, you'd structure it to describe the data itself:
<note>
<to>Self</to>
<from>Self</from>
<heading>Reminder</heading>
<body>Don't forget to buy milk!</body>
</note>
Notice that tags like <note> and <to> aren't standard. We made them up to describe our data. XML doesn't do anything on its own; it just holds the information in a structured way for another application to use. Here’s a quick breakdown:
| Feature | HTML | XML |
|---|---|---|
| Purpose | Displaying data | Storing & transporting data |
| Tags | Predefined (e.g., <h1>, <p>) | User-defined (e.g., <note>, <to>) |
| Focus | Presentation | Structure & meaning of data |
The Structure of an XML Document
All XML documents follow a few simple rules to stay organized. They have a hierarchical, tree-like structure.
First, an XML document often starts with an XML declaration. It's optional but recommended. It tells the parser which version of XML and which character encoding is being used.
<?xml version="1.0" encoding="UTF-8"?>
Next are the elements. An element is everything from its start tag to its end tag, including the content in between. For example, <heading>Reminder</heading> is a complete element.
Every XML document must have exactly one root element. This is the parent element that contains all other elements. Think of it as the main trunk of the tree.
Finally, elements can have attributes, which provide extra information about the element. Attributes are placed inside the start tag and consist of a name/value pair.
For example, instead of making <category> a separate element, we could make it an attribute of the <book> element:
<book category="cooking">
<title>Everyday Italian</title>
</book>
Here, category is the attribute name and "cooking" is its value.
A Complete Example
Let's put all these pieces together. Here is a simple XML document that describes a small collection of books.
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book category="children">
<title lang="en">Harry Potter</title>
<author>J. K. Rowling</author>
<year>2005</year>
</book>
<book category="web">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>2003</year>
</book>
</bookstore>
In this example:
<?xml ... ?>is the declaration.<bookstore>is the root element.<book>is a child element of<bookstore>.<title>,<author>, and<year>are child elements of<book>.categoryandlangare attributes.
Because the tags describe the content, the document is self-descriptive. You can understand what the data means just by reading it.
Now that you understand the basic structure of XML, let's test your knowledge.
What is the primary purpose of XML (Extensible Markup Language)?
Which of the following is a key difference between XML and HTML?
Understanding XML is a foundational skill for working with many types of data on the web and in software development. Its structured, self-descriptive nature makes it a reliable way to share information between different systems.