Syntax

XML Syntax

Here simple syntax rules to write in XML document.
<?xml version="1.0" encoding=?ISO-8859-1??>  
<note>  
<to>Ajay</to>  
<from>Arun</from>  
<heading>Reminder</heading>  
<body>Don't forget Mother birthday this weekend! </body>  
</note>
  • The first line syntax is the XML declaration. It defines the xml version and encoding used in the document.
<?xml version="1.0" encoding="ISO-8859-1?">
  • Next line describes the root element of document.
<note>
  • Next 4 lines describes the child element of the root elements.
<to>Ajay</to> <from>Arun</from> <heading>Reminder</heading> <body>Don't forget Abhay birthday this weekend! </body>
  • Last lines describes the end of the root element.
</note> It is case sensitive and begin with "<?xml>" where xml is written in lower case.

Example of XML: Movie

File: movie.xml
<Movies-store>    
<movie category="ACTION">    
            <title lang="en">Force</title>    
            <director>Nishikant Kamat</director>    
            <year>2011</year>    
            <budget>120 million</budget>    
    </movie>    
    <movie category="ROMANTIC">    
            <title lang="en">DDLJ</title>    
            <director>Yash chopra</director>    
            <year>1996</year>    
            <budget>40 million</budget>    
    </movie>    
    <movie category="COMEDY">    
<title lang="en">golmaal</title>    
<director>Rohit sheety</director>    
<year>2006</year>    
<budget>39.95</budget>    
    </movie>    
</Movies-store>
The root element in the example is <Movies-store>. All elements in the document are contained within <Movies-store>. The <Movie> element has 4 children: <title>, < director>, <year> and <budget>.

Example of Email

File: email.xml
<?xml version="1.0" encoding="UTF-8"?>    
<emails>    
<email>    
        <to>Ajay</to>    
        <from>Arun</from>    
        <heading>Hello</heading>    
        <body>Hello brother, Good morning!</body>    
</email>    
<email>    
        <to>Abhay</to>    
        <from>Amit</from>    
        <heading>New year wish</heading>    
        <body>Happy New Year Abhay</body>    
</email>    
<email>    
        <to>Raj</to>    
<from>Priya</from>    
<heading>Morning walk</heading>    
<body>Please start morning walk to stay fit!</body>    
</email>    
<email>    
        <to>Kunal</to>    
<from>Krishna</from>    
<heading>Health Tips</heading>    
<body>Smoking is injurious to health!</body>    
</email>    
</emails>



ADVERTISEMENT
ADVERTISEMENT