All source code on this website is released under the MIT License.
- Learn XML
Intro to XML
XML has become the de-facto standard for data interchange within and between applications. It consists of a wide array of supporting technologies, in addition to the design of XML documents themselves. Examples of the supporting technologies include XPath for navigating through the XML structure, XSLT for transforming XML into another format, and DTDs for declaring the structure and format of an XML file.
When first learning XML, looking at an XML file can seem very confusing or overwhelming. For the purpose of overcoming the apparent complicatedness of XML files, it’s worth noting that there are really only three main pieces coming together in an XML file. They are: Elements, either opening, closing, or self-closing (more on this in a bit). Attributes, which store information inside an opening or self-closing element. Text, inside of a set of opening and closing elements.
Here’s a simple first example of an XML document:
Apple Fruit Bread Grain
The first line is the XML declaration. It simply says “this document is xml, version 1.0”. For most cases, version 1.0 of the XML specification is fine; there is a version 1.1 but it usually offers little more value than 1.0.
The rest of the document is opening and closing tags, and text nodes in between tags. It is vitally important that outer tags fully enclose inner tags; which is to say, the following is not allowed in XML:
The preceding example shows a “fridge” tag closing before an inner “food” tag closes - resulting in an invalid XML structure. What this example lacks is “well-formedness”, that is, the proper basic structure that constitutes a properly formed XML document. One way of thinking of how an XML document is supposed to be structured is in a tree data structure, where each set of opening/closing tags (or each self-closing tag) is equal to one node in a tree.
A self-closing tag combines an opening and closing tag in one tag; it can be used when there are no child nodes for the tag (no child nodes means there are no tags inside of the tag). Here’s an example of a simple self-closing tag:
Often, self-closing tags will have attributes. Attributes sit inside of a tag, and provide a method of storing data within tags. The XML structure of a document can vary considerably based on the use of avoidance of attributes. Here are two sample XML documents - one using attributes, and one using only tags.
Example 1 - Tags
Intel i7 8GB
Example 2 - Attributes
<cpu brand=”Intel” model=”i7” /> <ram amount=”8GB” />
You can see that using attributes and self-closing tags can make an XML document easier to read and more compact. Ultimately, the choice is up to the creator whether to use attributes or not.
That concludes this section of the XML tutorial. In the next tutorial, we’ll take a look at doctypes and DTDs (Document Type Definitions), a way of declaring the expected structure of a given XML document.