Demystify XML and its application

Sourav.Kayal
Posted by in XML category on for Beginner level | Points: 250 | Views : 5538 red flag

This article is to get fundamental idea of XML and its uses and structure

What is XML? Why need to learn?

In this article we are going to look into what is XML and when it is needed to use

What is XML?

Let’s learn what XML is and why it’s needed to develop application? The acronym XML stands for “Extensible markup language”. If still you are reading I can assume that you are new in XML and want to know more about XML and its application in programming and application development.

Few years before when I was computer science student, one of my teacher asked question “Which programming language is truly platform independent?”—Answer came in different flavor like Java, C# etc. But he told us XML is truly platform independent. I thought “How it’s truly platform independent?”

And today when I use XML in day to day programming, I can realize his statement.  Yes, XML is perfect platform independent language.

XML is used to represent data and to transfer data from one system to another or across network and internet. And   almost all programming language can able to parse XML file to extract data from it.

Why XML is needed?

In above paragraph we looked at the basic purpose of XML. Now lets look at an example to clear our concept.

Two organizations want to make business with each other. And we are assuming that one organization is using MAC platform and another is using Windows. Now the business file generated in MAC platform may not support by windows application and vice versa. And this is the main problem of data exchange operation in network or internet environment. And I can guess that you are thinking the solution as XML. If you are thinking so, you are correct. XML is the solution. Thinking how?

MAC platform will represent its business data in XML format and windows application will parse and extract data from it and vice versa. Hence the data exchange problem has solved.

And as I told almost any programming language can parse xml file

How XML file looks like?
Let see how XML file looks like. Below is the example of xml.
<?xml version="1.0" encoding="utf-8" ?>
<Person>
    <name>Sourav</name>
    <surname>Kayal</surname>
    <age>25</age>
</Person>

 Now, you may think it’s very similar to HTML because it’s nothing but a set of tag. But those tags are unknown to you, right? And this is the beauty of XML. As I told earlier XML is used to represent data and to transfer data so there is no pre defined tag on it. According to data you can use your own tag.

 The first line is called prolog of XML. This line contains version and encoding information of XML.

 <?xml version="1.0" encoding="utf-8" ?>

Here my version of XML is 1.0 and encoding scheme is “utf-8”. Now what does it mean? It tells us which character will convert in which character when XML file will transfer across network.

The second line represent root node of XML document

<Persons>
</Persons>

And here you can notice that every tag has its closing tag. And if so then we can say that this XML document is well formatted document.

And next three elements are the child element of root element

    <name>Sourav</name>
    <surname>Kayal</surname>
    <age>25</age>

 Those three elements are containing actual data. When we will use other programming language to extract data from XML those data will come. This is very simple example of XML file in real life development you may get much heavier than that. Here the root element containing only three child element but in big XML file may contain more than one sub root element. Like below example.

<?xml version="1.0" encoding="utf-8" ?>
<Persons>
   <Person>
    <name>Sourav</name>
    <surname>Kayal</surname>
    <age>25</age>
  </Person>
  <Person>
    <name>scott</name>
    <surname>tiger</surname>
    <age>50</age>
  </Person>
 
  <Person>
    <name>system</name>
    <surname>admin</surname>
    <age>30</age>
  </Person>
</Persons>

 Here you can see <Persons> is the root element and <Person> is child of that. And each <Person> element containing three child element. Ultimately it will form a tree structure. Don’t forget that each and every XML document contain one any only one root element.


How to get output

Unlike HTML we cannot get any beautiful output from XML document. If you open any XML document in web browser you might experience same content just in tree view stricture. Then how we will get output from it?

Well, as I told earlier some programming languages need to use to get data from it and after getting it represent data in your own style. There is another way also, by using XSL or XML Style sheet language.

XML Syntax Rules

All XML Elements Must Have a Closing Tag

<name>Sourav</name>


XML Tags are Case Sensitive

XML tags are case sensitive. The tag <Letter> is different from the tag <letter>.

Opening and closing tags must be written with the same case:

XML Elements Must be Properly Nested

In HTML, you might see improperly nested elements:

<b><i>Sourav
Kayal</b></i>

In XML, all elements must be properly nested within each other:

<b><i>Sourav Kayal</i></b>


XML Documents Must Have a Root Element

<root>
 <Child>Data</Child>
</root>

XML Attribute Values Must be quoted

<Person country=”India”>

XML attributes

XML attributes provide additional information about element. Basically attributes content that information which is not part of actual data but attribute are important to manipulate data and identify element easily.

Here country is the example of XML attribute of Person element.

<Person country=”India”>

In parsing time we can use country attribute to get all child node value of Indian person.


Few rules of XML attribute

Attribute values must always be quoted. Either single or double quotes can be used. For a person's sex, the person element can be written like this:

<person
sex="female">
or like this:
<person
sex='female'>
If the attribute value itself contains double quotes you can use single quotes, like in this example:
<gangster name='George "Shotgun" Ziegler'>
or you can use character entities:
<gangster
name="Sourav &quot;Shotgun&quot; Kayal">

XML in real life

You may think that for long time we are discuss XML document, formatting and features but still now we didn’t discuss in real life scenario where XML is used. One popular uses of XML is RSS feed. You might have notice RSS feed option in various website. Those RSS feed represent their information (basically news) in XML format. And this XML data can transfer easily through REST and SOAP.

Another live example of XML is weather service. If you are aware of google or yahoo weather service you may find XML file there.

Page copy protected against web site content infringement by Copyscape

About the Author

Sourav.Kayal
Full Name: Sourav Kayal
Member Level: Silver
Member Status: Member,MVP
Member Since: 6/20/2013 2:09:01 AM
Country: India
Read my blog here http://ctrlcvprogrammer.blogspot.in/
http://www.dotnetfunda.com
I am .NET developer working for HelixDNA Technologies,Bangalore in healthcare domain. Like to learn new technology and programming language. Currently working in ASP.NET ,C# and other microsoft technologies.

Login to vote for this post.

Comments or Responses

Login to post response

Comment using Facebook(Author doesn't get notification)