In this guide, we will see few examples of XML document.
Simple XML document example
This is a simple XML document. You can understand the data, by just looking at the document. We have used self-describing tags.
<?xml version="1.0" encoding="UTF-8"?> <book> <name>A Song of Ice and Fire</name> <author>George R. R. Martin</author> <language>English</language> <genre>Epic fantasy</genre> </book>
The first line
<?xml version="1.0" encoding="UTF-8"?> is called XML Prolog. It is optional, however when we include it in the XML document, it should always be the first line of the document. XML Prolog defines the XML version and the encoding used in the XML document.
The tag <book> is the root of this XML document. A XML document should always have a root element and at the end of the document this root element needs a closing tag, just like </book> that we have used in the above example.
The tags <name>, <author>, <language> and <genre> are the child elements of the root element <book>. We will discuss more about these in the XML syntax tutorial.
Based on the above discussion we can say that a XML document structure looks like this:
<root> <child> <subchild>.....</subchild> </child> </root>
XML Example – Student data
Lets take a look at the another example of XML. In this XML document we have the details of the few students. Here <students> is the root element, <student> is the child element and name, age, subject and gender are sub-child elements.
<students> <student> <name>Rick Grimes</name> <age>35</age> <subject>Maths</subject> <gender>Male</gender> </student> <student> <name>Daryl Dixon </name> <age>33</age> <subject>Science</subject> <gender>Male</gender> </student> <student> <name>Maggie</name> <age>36</age> <subject>Arts</subject> <gender>Female</gender> </student> </students>