XML Elements

XML document is all about Elements and contents. Without elements, you can’t create XML document. Element is an identification which we are using after starting tag and before closing tag. Elements play very important role while designing XML, like building blocks.

It contains following details;


Sub Elements



Media Objects

Let’s understand it by using following example;


<ElementName attributes1=”value 1” attributes2=”value 2” attributes3=”value 3”>
.......Content part......
</ElementName >

Its name of the element, means identification for actual value which it store. Element must have their closing tag.

You may use multiple attributes for elements. Attribute is also known as property of the elements. It has pair of name and value. It can be like following
Name = “value” (Value must be in double quotes)

Empty Elements: XML elements which doesn’t have any content or value, that is known as empty element. You can use following syntax to define empty element.

<name></name >


<name / > (A self closing tag)

Note: An empty element can have also attributes.

Use the following example of XML document which has elements, attributes and sub elements.

<?xml version="1.0" encoding="UTF-8"?>
<products category=”Electronics” >
<name>Sony Xperia R1 Dual</name>

In above example;

<name>, <color>, <price> and <discount> elements have contents because they hold actual values for product details.

<products> and <product> are container type of elements because they have child elements.

<product> have an attributes with its name and value (category=”Electronics”).


While implementing elements in XML documents, you must follow below rules.

You cannot add any white space in element name

Elements names are case sensitive. You can’t use name and NAME for start and end tag.

Start and end elements must be same.

Element must start with alphanumeric. You can use Dot (“.”), Underscore (“_”) or Hyphen (“-“)

An element which contain sub elements and contents, it must be closed with nested structure.

You can’t use element name like “XML” or “xml” because it’s reserved word.

Naming Example for Elements:

Example Description Valid or Not Valid
<products> All letters are in lowercase Valid and best practice
<PRODUCTS> All letters are in uppercase Valid but not best practice
<products&> Contain special chars Not valid
<xml> Contain reserved word Not valid
<FirstName> Mix of upper and lower case Avoid this type of combination
<first_name> Using underscore Valid
<products:> Added semicolon Not valid
<first.name> Added period Avoid this type of combination
<first&last> Added Ampersand with two names Not Valid

Note: Other language character you may use but its fully depend on your application whether it parse XML document and process it or not.