XML
XML, or eXtensible Markup Language, is a markup language oriented around storing data. It is similar to HTML (for they derive from the same markup language), in that it uses tags, elements and attributes. However, XML doesn't use predefined tags; in other words, you make your own tags. Because of this, it is relatively easy to learn.
Prerequisites
editXML uses CSS to style itself, and is commonly used alongside HTML and JavaScript. Otherwise, XML is a text-based computer language, which be made and run by children that understand the alphabet and symbols (Age: 6+).
Parsing
editHTML automatically cleans up documents, and will attempt to continue if an error occurs. However, XML does not do this, so that code can be read clearly. You shouldn't worry about this, as the two simple rules are:
- Tags and elements are case-sensitive; a parser will refuse something such as:
<element>This is an element.<Element>
. - Tags and elements cannot start and have no end, and vice versa, like so:
<neverEnd>This element never ends.
.
Basic Structure
editAs an example, here is a template for a book in a catalogue using tags and elements:
<book>
<title>The Hobbit</title>
<author>J.R.R. Tolkien</author>
<date>9/21/1937</date>
<desc>A story about Bilbo Baggins, a hobbit, who goes on a journey.</desc>
</book>
The tag, book
, is what contains all the elements. Think of it like a box: all the information is inside it. Note that each tag and element ends with a backslash at the start of the name. This is what indicates the end of an element.
Then, we can add some attributes to add more information:
<book addDate="2/17/2021"> /* Date when the book was added to the catalogue */
<title titleStyle="old">The Hobbit</title>
<author>J.R.R. Tolkien</author>
<date>9/21/1937</date>
<desc>A story about Bilbo Baggins, a hobbit, who goes on a journey.</desc>
</book>
The <book>
and <title>
elements now have attributes, describing different things in a way similar to tags. Note that comments in XML are stated with similar starting and closing tags that elements use: /* This is a comment. */
.
XML tags can also be in another tag, as shown here:
<catalouge>
<book addDate="2/17/2021"> /* Date when the book was added to the catalogue */
<title titleStyle="old">The Hobbit</title>
<author>J.R.R. Tolkien</author>
<date>9/21/1937</date>
<desc>A story about Bilbo Baggins, a hobbit, who goes on a journey.</desc>
</book>
<book addDate="1/03/2021">
<title titleStyle="fantasy">Harry Potter and the Philosopher's Stone</title>
<author>J.K. Rowling</author>
<date>6/26/1997</date>
<desc>The first book in a series of a wizard who goes to school.</desc>
</book>
</catalouge>
XML also has a declaration, which gives information about the document:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
This declaration tag does not end, and is the only tag in XML that does so. The XML declaration tag has a few attributes of information:
version
: Defines the version of the tag. This is necessary, and the XML will not be able to be parsed.encoding
: Defines what text encoding it uses.UTF-8
is the default standard. This is not necessary, and the XML can be parsed without it.standalone
: Defines if the XML document includes an outside Document Type Declaration (DTD). If so, putno
. Otherwise, or if you are unsure, putyes
. This is not necessary, and the XML can be parsed without it.
Final Document
editWith all rules put into place, a final-draft XML document should look something like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<catalouge>
<book addDate="2/17/2021"> /* Date when the book was added to the catalogue */
<title titleStyle="old">The Hobbit</title>
<author>J.R.R. Tolkien</author>
<date>9/21/1937</date>
<desc>A story about Bilbo Baggins, a hobbit, who goes on a journey.</desc>
</book>
<book addDate="1/03/2021">
<title titleStyle="fantasy">Harry Potter and the Philosopher's Stone</title>
<author>J.K. Rowling</author>
<date>6/26/1997</date>
<desc>The first book in a series of a wizard who goes to school.</desc>
</book>
</catalouge>