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.
Example
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>
Example
<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>
Example
<person><info>Employee</info>Ricky</person>

Rules to write XML elements
a) Start and end tags must be the same.
Example:
<person><info>student</info>Bob</person>

b) XML document must have exactly one root element.
Example:

<c>
     <a>...</a>
     <b>...</b>
</c>

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.
Example:
<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.

Example:
<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

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


Output:
number entities

6. Comments

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

7. Content

Content represents the information of elements in XML document.

Example
<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

<STUDENT>
     <SAM></SAM>
     <BOB></BOB>
</STUDENT>


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

<ICECREAM>
     The ICECREAM is available in 2 flavors.
     <FLAVOR>Chocolate</FLAVOR>
     <FLAVOR>Butterscotch</FLAVOR>
</ICECREAM>


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