Wednesday, July 9, 2014

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
<?xml version="1.0" encoding="UTF-8"?>
<mobilephones>
    <mobilephone>
        <model>D6563</model>
        <name>Xperia Z2a</name>
        <brand>Sony</brand>
        <released-year>2014</released-year>
        <dimensions>
            <height>137mm</height>
            <width>72mm</width>
            <depth>11mm</depth>
        </dimensions>
        <weight>163</weight>
    </mobilephone>

    <mobilephone>
        <model>VS876</model>
        <name>Lucid 3</name>
        <brand>LG</brand>
        <released-year>2013</released-year>
        <dimensions>
            <height>131.6mm</height>
            <width>66mm</width>
            <depth>9.9mm</depth>
        </dimensions>
        <weight>123.9</weight>
    </mobilephone>
</mobilephones>


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.

<!-- This is a comment -->

Empty elements in XML

<empty_tag></empty_tag> can be replaced by <empty_tag/>

Attributes 

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

Eg: 



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

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
<student>
 <name>Anne</name>
 <age>29</age>
 <mobiles>
  <mobile>071xxxxxxx</mobile>
  <mobile>078xxxxxxx</mobile>
 </mobiles>
</student>

In this example you can see '<mobiles>' 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/">
</element>



Hope to see you with the next XML session...

0 comments:

Post a Comment