Gentle Introduction to XML

XML stands for Extensible Markup Language, which is used for businesses to data in electronic format. Unlike html, there are no predefined tags in XML. XML lets users to define their on tags to describe their data. Say, a user wants to create a shopping list. She can create her own tags. She can use the tag name <list> to describe the list. Then use the tag <item> to describe each item. For each item, she can use the tag <name> to describe name and <qty> to describe quantity.

So the XML looks like this:

<?xml version="1.0"?>
<list>
<item>
   <name>Noodles</name>
   <qty>3</qty>
</item>
<item>
   <name>Sugar</name>
   <qty>1lb</qty>
</item>
</list>

If a user wants to use the tag, <quantity> instead of <qty>, she is free to do so as there are no predefined tags in XML. It is up to the user to decide what each tag name means. If you have noticed the line, <?xml version="1.0"?>, that is the standard declaration line in all xml files.

XML Syntax

All XML files should follow the following syntax rules:

  1. The XML file should have exactly one root element that contains all xml. In the above example, root element is <list>, which contains everything else.
  2. All tags must be closed. (e.g., <name> should be followed by </name>)
  3. No overlapping tags. All tags must be correctly nested. <name> </name> <qty> </qty> is fine where as <name> <qty> </name> </qty> is not as the tags are overlapping. <name> <qty> </qty> </name> is fine syntactically as well although it probably doesn't make much sense in the above context.
  4. Tag name cannot contain spaces. Tag name should begin with an alphabet or _ character. Tag names are case sensitive.
  5. All attributes should be within double quotes (" ").
  6. The declaration line should be the very first in an XML file.

You may share this article on Facebook.