XML Basics

XML

Basic XML

Hi,
I would like to represent you about the basics of XML which I came across by Googling and participating the WSO2 Boot Camp.

Let’s start with the most important question.

What is XML?

  • XML stands for eXtensible Markup Language.
  • Unlike HTML(Hyper Text Markup Language), XML is for carrying data and not for displaying data.
  • XML is not a database, but it can store data.
A good definition I found from w3schools about XML is, XML is a software- and hardware-independent tool for carrying information.

XML does not have any predefined tags. Users can define their own tags. If you just double click on a XML document, your browser just display the text. 

Sample XML code

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28


    
        D6563
        Xperia Z2a
        Sony
        2014
        
            137mm
            72mm
            11mm
        
        163
    

    
        VS876
        Lucid 3
        LG
        2013
        
            131.6mm
            66mm
            9.9mm
        
        123.9
    

You can notice the first line of this document. Actually the XML document itself does not need it. That line is for the XML parser to identify this document as an XML file. We will discuss about ‘parser’ in the next post.

A well formatted XML document

  • Every staring must have a matching ending tag.
  • The elements must be properly nested. (The last opened tag must close first.)
  • None of the special characters such as ‘<‘ and ‘&’ must not be in the values.
  • A single root element must contain all the elements.
  • Tags are case sensitive, so we need to use correct format for all starting and ending tags.
  • The attribute values must be quoted. 

Comments in XML

The syntax is similar to HTML comments.


Empty elements in XML

can be replaced by

Attributes 

Elements can have attributes. Mostly they are for representing meta data.

Eg: 


<student name="Anne" age="29" mobile="071xxxxxxx">

Bur it is encouraged to avoid using attributes. Because,

  • it gives a flat structure 
  • cannot have multiple values for a single attribute.
  • hard to expand. 

Assume we need to insert another mobile number for the student ‘Anne’. Using attributes it is not possible. Here is the solution for that.


1
2
3
4
5
6
7
8

 Anne
 29
 
  071xxxxxxx
  078xxxxxxx
 

In this example you can see ” tag has been introduced. In XML those elements must be wrapped with a single sub root element.

Namespaces in XML

XML namespaces are used to provide uniquely identified names and attributes in an XML document.

1
<e:element xmlns:e="http://www.element.org/element">

And also we can use default namespace to avoid using the prefix every time in sub elements.


1
2
<element xmlns="http://www.w3.org/TR/html4/">

Hope to see you with the next XML session…

Leave a comment

Your email address will not be published. Required fields are marked *