XML Structure

Structure of XML

XML document consists of a number of components used to represent information in hierarchical manner. These components are as follows.

1. Processing Instruction

  • XML document begins with processing instruction (PI).
  • PI indicates the version of XML and it is optional.
PI statement: <?xml version=“1.0”?>

Rules of XML PI declaration
a) XML declaration are case sensitive.
b) XML declaration appear at very first line.

2. Tags

  • Tags are used to identify the data.
  • XML tags begin with “<” and end with “>”

  • Start tag: <element>
    End tag: </element>
<person> Name</person>

3. Elements

  • Elements are used to identify and describe  the XML data.
  • Start with a start tag, <element> and End with </element>

Rules to write XML elements
a) Start and end tags must be the same.

b) XML document must have exactly one root element.


In the above example, c is root element and elements a and b are within the root element c

c) XML elements are case sensitive.
<name>Bob</name> and not as <Name>Bob</name>

4. Attributes

Attributes are name-value pairs and occur inside start-tags after the element name.

<student gender =“male”>

In above example, gender is an attribute and student is an element.

5. Entities

  • Entities are used to represent the special characters.
  • Every entity should have a unique name.
  • Entity names begin with the &(ampersand) and end with semicolon(;).
Internal Entities:
  • Internal entities associate a name with string of literal text.
  • Internal entities define the shortcuts for frequently typed text or frequently changing text.
The XML predefines internal entities which are as follows:
a) & lt; used for less than symbol, <
b) & gt; used for greater than symbol, >
c) & amp; produces ampersand , &
d) & apos; used for a single quote character (apostrophe),'
e) & quot; used for double quote character, “ ”

Example : Internal entities

     <LARGE>50 & gt; 5</LARGE>
     <SMALL> 5 & lt; 50</SMALL>

number entities


  • XML comments begin with <!--  and End with - - >
  • Comments are not part of the textual content of XML document.
<!- - - -comment- - - ->

7. Content

Content represents the information of elements in XML document.

<Bookname>Java and XML</Bookname>

In the above example, the name of book Java and XML is the content of the Bookname.

An element contains three types of content.

a) Character: Contains only textual information
b) Element Content: One element contains other elements.

Example : Element Content


In the above example, the element STUDENT contains two elements, SAM and BOB.
c) Combination content: Elements contain combination of textual information and other elements.

Example : Combination content

     The ICECREAM is available in 2 flavors.

icecream content
In the above example, the ICECREAM element contains combination of textual information and flavor element.