BeginnersBook

  • Home
  • Java
    • Java OOPs
    • Java Collections
    • Java Examples
  • C
    • C Examples
  • C++
    • C++ Examples
  • DBMS
  • Computer Network
  • Python
    • Python Examples
  • More…
    • jQuery
    • Kotlin
    • WordPress
    • SEO
    • JSON
    • JSP
    • JSTL
    • Servlet
    • MongoDB
    • XML
    • Perl

XML Example

Last Updated: October 24, 2018 by Chaitanya Singh | Filed Under: XML Tutorial

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>

Top Related Articles:

  1. JSON vs XML
  2. XML Validator
  3. XML DTD
  4. XML Comments
  5. HTML vs XML

About the Author

I have 15 years of experience in the IT industry, working with renowned multinational corporations. Additionally, I have dedicated over a decade to teaching, allowing me to refine my skills in delivering information in a simple and easily understandable manner.

– Chaitanya

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Copyright © 2012 – 2025 BeginnersBook . Privacy Policy . Sitemap