6.1. Elements
The nodes shown in Listing 6-1 are called elements, and they closely resemble HTML tags. And like HTML tags, start tags begin with < and end tags begin with </. However, unlike HTML tags, all XML tags must either have a closing tag or be self-closing or empty elements. Self-closing tags are recognizable by the ending />. If the forward slash was omitted, the document would not be a well-formed XML document. In addition to all elements being either closed or self-closing, the tags must always match up in order. This means that the XML document in Listing 6-2 is well formed, whereas the XML document in Listing 6-3 is not well formed.
Listing 6-2. A Well-Formed XML Document
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<one>
<two>
<three>
<four/>
</three>
</two>
</one>
|
Listing 6-3. A Document That Is Not Well Formed
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<one>
<two>
<three>
<four/>
</two>
</three>
</one>
|
So far, we have covered elements that contain either other elements or empty elements, leaving the question of what elements that contain actual data look like. Using the XML from Listing 6-1 as a starting point, you can see that the answer is not very different. Listing 6-4 shows what elements that contain text data look like.
Listing 6-4. An XML Document with Text Data
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<library>
<book>
<series>The Lord of the Rings</series>
<title>The Fellowship of the Ring</title>
<author>J.R.R. Tolkien</author>
</book>
<book>
<series>The Lord of the Rings</series>
<title>The Two Towers</title>
<author>J.R.R. Tolkien</author>
</book>
<book>
<series>The Lord of the Rings</series>
<title>The Return of the King</title>
<author>J.R.R. Tolkien</author>
</book>
</library>
|
One thing to remember is that elements aren't limited to containing either other elements or text data; they can do both at the same time. In fact, there is even a way for empty elements to contain text data through the use of attributes.
|