0
Introduction
XML, or Extensible Markup Language, rounds out the usual suspects of YAML, JSON, and XML. It’s probably my least favorite of the three, but knowledge of XML is needed when working with code.
XML is, of course, related to HTML. So why didn’t we just settle for HTML? Turns out machines don’t understand HTML very well. They can parse it perfectly fine, yes, but in HTML you put information in, such an address, and you understand it’s an address because you are a human. A machine doesn’t know that Baker Street is an address, unless you tell it.
Syntax
XML consists of tags, elements, and attributes. Let’s take a basic example and then go through these in more detail:
<?xml version="1.0" encoding="UTF-8"?>
<address>
<name>
<title>Mr</title>
<first-name>Sherlock</first-name>
<last-name>Holmes</last-name>
</name>
<street>221B Baker Street</street>
<city state="NA">London</city>
</address>
First, we declare that this is an XML document and the encoding used. This is called a prolog. It’s optional, but if included, should always be the first line.
The tag <address> is the root of the document. We must always have a root. The tag <address> has three children:
The tag <name> has three children as well: