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
<?xml version="1.0" encoding="UTF-8"?>
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/>
Elements can have attributes. Mostly they are for representing meta data.
<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.
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.
And also we can use default namespace to avoid using the prefix every time in sub elements.
Hope to see you with the next XML session…