Share on Google+Share on Google+

XML Syntax Rules

The syntax rules for XML are very simple and strict. These are easy to learn and use. Because of this, creating software that can read and manipulate XML is very easy.

XML Syntax Rules


The syntax  rules  for  XML are very simple and strict. These  are  easy to learn and  use. Because of this, creating software that can read and manipulate XML is very easy. Xml enables an user to create  his own tags.

Note - XML documents use a self-describing and simple syntax







Let's develop a simple XML document :

<?xml version="1.0" encoding="ISO-8859-1"?>
<Body>Be ready for a cruise...i will catch u  tonight</Body>

The XML declaration:  Always the first line in the xml document: 

The XML declaration should always be included. It defines the XML version and the character encoding used in the document. In this case the document conforms to the 1.0 specification of  XML and uses the ISO-8859-1 (Latin-1/West European) character set.

<?xml version="1.0" encoding="ISO-8859-1"?>

Root Element: The next line defines the first element of the document . It is called as the root element 


Child Elements: The next 4 lines describe the four child elements of the root (To, From, Subject and Body).

<Body>Be ready for a cruise...i will catch u  tonight</Body>

And finally the last line defines the end of the root element .


you may feel from this example that the XML document contains a  E-mail To Rohan  From Amit. Don't you agree that XML is quite self-descriptive?

Now let's discuss its syntax-rules which are very simple to learn.

All XML elements must have a closing tag

In XML all the elements must have a closing tag like this:


XML tags are case sensitive

XML tags are case sensitive. The tag <To> is different from the tag <to>.Hence the opening and closing tags must  be written with the same case:


XML Elements Must be Properly Nested

Improper nesting of tags makes no sense to XML. In XML all elements must be properly nested within each other like this in a logical order:

<b><i>Hi , how are you.....</i></b>

XML Documents Must Have a Root Element

All XML documents must contain a single tag pair to define a root element. All other elements must be written within this root element. All elements can have sub elements called as child elements. Sub elements must be correctly nested within their parent element:


Always Quote the XML Attribute Values 

In XML the attribute value must always be quotedXML elements can have attributes in name/value pairs just like in HTML. Just look the two XML documents below.  

The error in the first document is that the date and version attributes are not quoted .
<?xml version=1.0 encoding="ISO-8859-1"?>
<E-mail date=12/11/2002/>

The second document is correct:

<?xml version="1.0" encoding="ISO-8859-1"?>
<E-mail date="12/11/2002"/>

With XML, White Space is Preserved

With XML, the white space in a document is  preserved .

So a sentence like this :   Hello  How are you, will be displayed like this:

Hello  How are you,

Comments in XML

The syntax for writing comments in XML is similar to that of HTML.

<!-- This is a comment -->



Posted on: May 24, 2007 If you enjoyed this post then why not add us on Google+? Add us to your Circles

Share this Tutorial Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Discuss: XML Syntax Rules   View All Comments

Post your Comment

Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
June 4, 2012
why we use

<?xml version="1.0" encoding="ISO-8859-1"?> why are we using ? on this coding and what the meaning of ?