So far, weve covered some of the rules for creating XML documents. Weve looked at the different types of content within an XML document and seen some XML fragments . Now its time to put these rules together to create a complete XML document.
The following listing shows a simple XML document based on the phone book that I talked about earlier. I use the example throughout the rest of this chapter.
<?xml version="1.0"?> <phoneBook> <contact id="1"> <name>Sas Jacobs</name> <address>123 Some Street, Some City, Some Country</address> <phone>123 456</phone> </contact> <contact id="2"> <name>John Smith</name> <address>4 Another Street, Another City, Another Country</address> <phone>456 789</phone> </contact> </phoneBook>
Ive saved this document in the resource file address.xml .
The first line declares the document as an XML document. The declaration is not required, but its good practice to include it. A software package that opens the file will immediately identify it as an XML document.
The remaining lines of the XML document contain elements. The first element, <phoneBook> , contains the other elements: <contact> , <name> , <address> , and <phone> . There is a hierarchical relationship between these elements.
There are two <contact> elements. They share the same parent , <phoneBook> , and are child nodes of that element. They are also siblings to each other.
The <contact> tag is a container for the <name> , <address> , and <phone> elements, and they are child elements of the <contact> tag. The <name> , <address> , and <phone> elements are grandchildren of the <phoneBook> element.
Youll notice that the last line is a closing </phoneBook> tag written with exactly the same capitalization as the first tag.
Each <contact> tag has a single attribute id . Attributes normally provide extra information about a specific element, in this case a unique identifier for each <contact> .
As we saw earlier, the element
can be rewritten as
<contact> <id>1</id> </contact>
In this document tree, the trunk of the tree is the <phoneBook> tag. Branching out from that are the <contact> tags, and each <contact> has <name> , <address> , and <phone> branches.
Figure 2-2 shows the relationship between the elements in the phone book XML document.
In this example, Ive created my own tag names. The names Ive chosen tell you about the type of information that Im working with so its easy to figure out what Im describing.
If I want to share the rules for my phone book XML document with other people, I can create a DTD or XML schema to describe how to use the tags. Adding a reference to the DTD or schema will ensure that any XML documents that I create follow the rules. This process is called validating an XML document. Ill look at working with DTDs and schemas in the next chapter.
I can view an XML document by opening it in a web browser. Figure 2-3 shows address.xml displayed in Internet Explorer.
You can see that Internet Explorer has formatted the document to make it easier to read. It has also added some minus signs that I can click to collapse branches of the document tree.