An XML sample

Home » xml » An XML sample
xml No Comments

A sample of what xml can do

XML documents, like HTML documents are comprised of tags and data. The big difference between these is that the tags in html are already created (you just need to use them) and in xml the author of the document creates his own tags that describe the data according to his needs.

Another difference between the two is that xml is designed to store and describe data (described means that the code is self explanatory also known as human readable), it is not designed to display data like HTML.

Please take a look at the following examples to get a better idea of how XML documents look like.

Example 1

<?xml version="1.0"?>
<wonder>
<name>Lighthouse of Alexandria</name>
<ancient_name>Pharos of Alexandria</ancient_name>
<location>Pharos, Alexandria, Egypt</location>
<height units="feet">394–449</height>
</wonder>

Example 2

<?xml version="1.0"?>
<ancient_wonders>
<wonder>
<name>Great Pyramid of Giza</name>
<ancient_name>Khufu's Horizon</ancient_name>
<location>Giza pyramid complex, Giza, Egypt</location>
<height units="feet">481</height>
</wonder>
<wonder>
<name>Lighthouse of Alexandria</name>
<ancient_name>Pharos of Alexandria</ancient_name>
<location>Pharos, Alexandria, Egypt</location>
<height units="feet">394–449</height>
</wonder>
</ancient_wonders>

The first line of the XML document <?xml version=”1.0″?> is the XML declaration also knows as the prologue (only in some circles) which notes which version of XML you are using.

As you can see the first example is comprised in the second, let me expand on that. The first document contains just one of the wonders of the world, so we would need seven documents for each of the seven wonders, but the second was designed to hold all seven of them, I only included two of them in the second example, I’ll explain the second one.

The next line begins the data part of the document and is called the root element.In an XML document, there can be only one root element.

After the root we will have the first children / child elements, the elements, they each contain the elements that describe one of the wonders, their children elements are , , and .

The last child element, height, contains an attribute called units which gives us the type of unit of measure, in our case we have “feet”.

The last line in an xml document is the closing tag of the root element, in our case . And that was it, nothing else needs to be written, added or annotated.

LEAVE A COMMENT