First off, XML is not a computer language. Rather, it is a metalanguage for defining or specifying how to mark up a document in such a way as to identify its structure.
Say what?
How about this definition: XML is a method of arranging a document so that it's broken up into parts. For example, in this chapter you're going to create an XML document of role-playing monsters. The document will be broken up by monster name, hit dice, and weapon(s). (If you play Dungeons & Dragons [D&D], you know this is a very small subset of all the information available, but I didn't want or need to make the examples any more difficult.)
XML documents, in their simplest form, are made up of a hierarchy of two types of components: elements and attributes.
An element is made up of three parts:
Start element node, often called the start tag. It is made up of an element text name enclosed in angle brackets: <Element_Tag>.
Content node(s) made of a combination of zero or more text nodes (text enclosed between start and end element nodes) and child or nested elements (hence the hierarchical nature of XML).
End element node, often called the end tag. It is made up of a backslash and text, which must exactly match the text name of the start element node, enclosed in angle brackets: </Element_Tag>.
An attribute is an extension to the start element node. It provides more information about the element. Attributes are one or more name= "value" pairs added after the element text name but before the closing angle bracket: <Element_Tag name="value" >.
Two additional components that you will encounter are the XML header declaration and the comment. The header declaration indicates that the file should be parsed as XML and in most cases will simply read
<?xml version="1.0" encoding="utf-8"?>
Comments provide the reader of the XML file additional information that will be ignored by the XML parser. The syntax of a comment is <!-- comment_text -->.
Listing 13-1 shows the XML document that you'll be using throughout the chapter.
Listing 13-1: An XML Monster File
<?xml version="1.0" encoding="utf-8"?> <!-- Monster List --> <MonsterList> <!-- Easy Monster --> <Monster> <Name>Goblin</Name> <HitDice Dice="1d8" Default="4"/> <Weapon Number="1" Damage="1d4">Dagger</Weapon> </Monster> <!-- Medium Monster --> <Monster> <Name>Succubus</Name> <HitDice Dice="6d8+6" Default="33"/> <Weapon Number="2" Damage="1d3+1">Claw</Weapon> <Weapon Number="1" Damage="1d4">Dagger</Weapon> </Monster> <!-- Tough Monster --> <Monster> <Name>Red Dragon</Name> <HitDice Dice="22d12+110" Default="253"/> <Weapon Number="1" Damage="2d8">Bite</Weapon> <Weapon Number="2" Damage="2d6">Claw</Weapon> <Weapon Number="2" Damage="1d8">Wing</Weapon> </Monster> </MonsterList>