Almost all the XML examples that I’ve seen use an address book example, but just to be different (and to make things interesting), we are going to create a XML file that contains information about the images in a folder and we will use this to create a very simple gallery.
The first step is to decide what information we want to store about the image. There are the usual suspects (the file name, the name of the image, the size) and we want to make sure this information is in our XML file. However, the most important thing to remember is the root tag. This will be the tag that will start and end our XML file. Lets call this tag <imageinfo>
The next step is to decide on what attributes a tag will have. An attribute is like a property of the tag. For example, the <img> tag in HTML has the attribute src which tells browser where to find the image. For our example, we will give the size tag the attributes of width and height.
Most of the time developers are more concerned with parsing XML files rather than writing them, however, knowing the basics of what to expect in a XML file helps when trying to debug a parser.
Let write our very basic XML file :
<?xml version="1.0"?> <imageinfo> <image> <filename>AmericanPie.jpg</filename> <size width="300" height="300" /> <name>Mmm...Pie</name> </image> <image> <filename>Cantaloupe.jpg</filename> <size width="300" height="300" /> <name>Cantaloupe</name> </image> <image> <filename>CitrusSlices.jpg</filename> <size width="300" height="300" /> <name>Citrus For Summer</name> </image> </imageinfo> |
<?xml version="1.0"?> must be the first tag in a XML file. It is called the xml declaration and identifies the file as a XML file to a parser.