XML Tags

As we already looked into previous chapter, let’s learn more about XML tags.

Tags are essential part of any XML document. They play very important role while you are designing or creating your XML document. We cal also ways, its foundation of XML. Tags define what is the scope of elements in XML document. Moreover, they are used to write comments, declare setting and also to add any special kind of notes in XML document.

Mainly there are three types of tags in XML.

Start or Opening Tag: <products> (Beginning of every XML elements)

Closing or End Tag: </products> (Closing of every XML elements)

Blank or Empty Tag: The text between start tag and end tag called actual value or text of elements. The elements which doesn’t have any values or texts known as empty elements. It can be used with two options.

<hr></hr>

OR

<hr />

The start tag followed by closing tag. OR, you can use in other format like <hr />

This element tags doesn’t have any values.

There are mainly two rules which you need to follow while using tags in XML document.

Tag Rule 1:

As previously mentioned, XML tags are case sensitive. Lets understand correct and incorrect case sensitive tag example as below;

<name>Sony Xperia R1 Dual</name>          =       Correct

<Name>Sony Xperia R1 Dual</name>          =       Incorrect

Tag Rule 2:

Tags should be closed with proper order. Kind of like nested. Element Tag must be closed before closing its parent element tag. Lets understand with following example.

<?xml version="1.0" encoding="UTF-8"?>
<products>
<product>
<name>Sony Xperia R1 Dual</name>
<color>Silver</color>
<price>$650.00</price>
<discount>$20.00</discount>
</product>
</products>

In above example, you can see <products> and </products> are starting and closing to define one product details. Now, in that you cannot start element inside both of them and close it out side somewhere in XML.

Let’s look into example to understand it clear.

Correct Example:

<products>
<product><name>Sony Xperia R1 Dual</name></product>
</products>

Incorrect Example:

<products>
<product><name>Sony Xperia R1 Dual</product></name>
</products>