Thursday, 7 November 2013

A Lesson On XMLs


XML stands for Extensible Markup Language.
It is a markup language much like HTML (click here!) however it does not replace the latter. Rather, they complement each other.
In most web applications, XML is used to transport data while HTML is used to format and display the data, with focus on how the data looks. XML was created specifically to just structure, store, and transport the information.
With XML, your data can be available to all kinds of "reading machines" (Handheld computers, voice machines, news feeds, etc), and makes it more available for blind people, or people with other disabilities.



How To Write Your Own XML Document
An example of an XML document:


XML Document (Tree Structure)
XML documents form a tree structure that starts at "the root" and branches to "the leaves".
The tree starts at the root and branches to the lowest level of the tree.
All elements can have sub elements (child elements):
<root>
  <child>
    <subchild>.....</subchild>
  </child>
</root>
Children on the same level are called siblings (brothers or sisters).
All elements can have text content and attributes (just like in HTML).
The image above represents one book in the XML document below:
<bookstore>
  <book category="FICTION">
    <title lang="en">Angels And Demons</title>
    <author>Dan Brown</author>
    <year>2001</year>
    <price>35.00</price>
  </book>
  <book category="RELIGIOUS">
    <title lang="en">Most Common Questions Asked By Non Muslims</title>
    <author>Dr. Zakir Naik</author>
    <year>2012</year>
    <price>11.00</price>
  </book>
  <book category="MATHEMATICS">
    <title lang="en">50 Mathematical Ideas</title>
    <author>Tony Crilly</author>
    <year>2007</year>
    <price>50.00</price>
  </book>
</bookstore>

Rules in XML
There are, however, some rules to follow when writing your XML documents.
No. Rules
1. All elements must have a closing tag.

E.g. <p>This is a paragraph</p>

        <p>This is another paragraph</p>

2. Opening and closing tags must be written with the same case.

E.g. <Message>This is incorrect</message>

        <message>This is correct</message>

3. All elements must be properly nested within each other.

E.g. <b><i>This text is bold and italic</i></b>


4. Must contain one element that is the parent of all other elements.

5. Attribute values must always be quoted.

E.g. <note date="12/11/2007">

           <to>Michael</to>
           <from>John</from>
       </note>

6.Replace a character like "<" inside an XML element with an entity reference.

E.g. <message>if salary < 1000</message> => Error

       <message>if salary  &lt;  1000</message> => Correct

There are 5 predefined entity references in XML.




We hope that you have gained more knowledge about the XML from this post, and will try to make your very own. Be creative and if writing codes is outside of your comfort zone, don’t give up! You can do it!
“Don’t be afraid if things seem difficult in the beginning. That’s only the initial impression. The important thing is not to retreat; you have to master yourself.”
- Olga Korbut
If you would like to get your hands on more information on XML, please find a link and a video located below:

Enjoy!

0 comments:

Post a Comment