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.


 

Ads

Tutorials   
JAXB Create XML File And Get Data From XML XML,XML Tutorials,Online XML Tutorial,XML Help Tutorials The Simple API for XML (SAX) APIs Part I XML,XML Tutorials,Online XML Tutorial,XML Help Tutorials XML Tutorial Ignoring Comments While Parsing an XML File Accessing XML file from Java Preventing Expansion of Entity References Getting next Tag in the XML File Replacing a Node with a New One Reading XML Data from a Stream Reading an XML document using JDOM Query XML with an XPath expression Normalize All of the Text in a Document Modifying Text by Replacement Insert a Processing Instruction and a Comment Node Getting text values from a NodeList Finding an ID Value and Print the Element Text Edit Text by Insertion and Replacement Duplicating a Subtree Deleting Two Attributes Delete the First Child of the Root Node Creating XML Tree Creating a DocumentFragment Subtree and Appending it to the Document Creates element node, attribute node, comment node, processing instruction and a CDATA section Creates a New DOM Parse Tree Replacing a Text Node with a New CDATA Section Node Create an Empty DOM Document Adding an Attribute in DOM Document Transforming XML with XSLT Transforming an XML File with XSL Transforming XML with SAXFilters Storing properties in XML file Testing EntityReferences in Xml Splitting One Text Node into Three Reading XML from a File Modifying Text by Cutting and Pasting Locating a Node by Using Siblings Locate a Node and Change Its Content Loading properties from a XML file Listing nodes used in a document Inserting a New Entry in a List Creating an InlineStreamFilter Ignoring Comments While Parsing an XML File Handling Errors While Parsing an XML File Emitting DOCTYPE Declaration while writing XML File Converting CDATA Nodes into Text Nodes Java DOM Tutorial Creating Blank DOM Document
Ads

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"?>
<E-mail>
<To>Rohan</To>
<From>Amit</From>
<Subject>Surprise....</Subject>
<Body>Be ready for a cruise...i will catch u  tonight</Body>
</E-mail>

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 

<E-mail>

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

<To>Rohan</To>
<From>Amit</From>
<Subject>Surprise....</Subject>
<Body>Be ready for a cruise...i will catch u  tonight</Body>

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

</E-mail>

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:

<To>Rohan</To>
<From>Amit</From>

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:

<To>Rohan</To>
<to>Rohan</to>

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:

<root>
  <child>
  <subchild>.....</subchild>
  </child>
</root> 

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

     

Advertisements

Share on Google+Share on Google+

XML Syntax Rules

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

Advertisements

 

Discuss: XML Syntax Rules   View All Comments

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:1
pratibha
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 ?

Ads

 

Ads