XML coding tutorial for beginners – Part 1

Welcome to the XML tutorial for beginners. In this video, we will understand the XML format, understand the XML structure and know how to create an XML document. So let us get started. So first of all introduction to XML very quickly. XML stands for Extensible Markup Language. So basically it is a markup language in which we can define our own tags to store the data and it is also extensible meaning that once we have created our XML document, we can extend it to store more data also. We will see examples of XML document and how to extend it in this video. XML is meant to store transport and exchange data. So we can store XML data as strings or in text files or even in database tables. XML can be used to transport data across the network and we can use XML to exchange data between software applications or between machines. XML is a software-independent format for exchanging data. So it is not any proprietary format of any particular application. It is basically a very simple and readable text format. There is a distinction between HTML and XML. HTML can store and display data but XML simply stores the data. So with XML, the advantages that we can store over data in the XML format and we can have HTML which reads the data in the XML and displays it. So with this introduction let us see an example. The example that we are going to see is an XML document which stores our emails. So first of all we have to put the XML declaration in our document. It is also called the prologue and it starts with an angle bracket question mark. And we have to give the XML version. 1.0 is a more popular version, and we can give the encoding also. Encoding means that we can store international characters in our data also. The next thing we have to put in our XML document is the root element. An XML document has exactly one root element so everything else apart from the XML declaration will come within this root element. Since we are going to have our messages data, we have another XML element called message. Then we can have other XML elements like… So this whole string starting from the beginning tag to the ending tag including the data is our XML element. So XML elements contains the starting tag. it contains the data and it contains the ending tag also. So we have given the data for the to in our email message. We can give from also and from is again another XML element. It contains some other data for this particular message. Then we can give the subject XML element. Then give we can give the body XML element and we can give the attachment XML element. So, once we have all this XML created then we can open it in the browser and see how it looks like. One thing to note over here is that there is something called a parent element, a child element and a sibling element. So what is the parent? So, if we see the emails… Emails is the parent of the message XML element. Why? Because the message XML element is totally within the emails XML element so emails is: starting tag is here and emails ending tag is here and if we see the message element, message starting tag is here and message ending tag is here. So, that is why emails is a parent of the message XML element. Similarly, the message XML element is the parent of the to XML element. Why? Because the to XML element is totally within the message XML element. The to XML element and the from XML element are at the same level. They are both child elements of the message element. So that is why they are called siblings. So to element is the sibling element of the from element and the from element is a sibling XML element of the to XML element. So, let us see an example of how we can write our XML. You can write XML in any text editor (notepad or I am using notepad++). So first of all we have to give the XML declaration so as I showed, it gives the XML version and an optional encoding. Then we have to give the root element. So there is exactly one root element. Everything else is going to come within the root element. So we have a single message, so we put the message starting tag and the message ending tag. This is the message starting tag. And this is the message ending tag. Here then within the message XML element, we can put the to XML element. Also we need who the messages is from. So we put the from XML element. We also need the subject XML element to tell what is the subject of this message. Then we need the body XML element to show what is contained in the body of this message and there can be an attachment or not. So this is our complete XML document. So what we can do is we can open it in the browser and see how it looks like. So this is how it looks like and you can see at the very top, we have the emails, which is the root element. And we can expand and collapse the different elements. So within emails, we have the message XML element and within message, we have the to data, from, subject, body and attachment. Now one other thing we can do is that we can extend this XML. What is the need of extending the XML? So let us say, that we have a software application that reads this XML document, and it is able to show the message with the data of to, from, subject, body etc. So let us say at a later date, we want to store the date of the message also. So all we need to do is add more XML element for the date like this. And we can put the date also. And now if we open it in the browser, then it is going to show the date also. So this way we can extend our existing XML document and the applications that are reading this XML document will continue to read it because whatever data they are expecting – to, from subject body attachment that already exists, but newer applications can read the newer data, which is the date of the message also. So let us go back now. Now, we can understand the XML structure by using the tree analogy. So let us imagine that this is the root of the tree. So it is similar to the root element in the XML. Now, the root can have child. It can have no children, or it can have one child or it can have more than one children. And similarly, a child element can further have either zero children or one child or multiple child elements. So here for example, this child does not have any child elements. But this child has two child elements which are like the branches of the tree. So a child can have multiple child elements also. So this is the child and it has 1 2 3 4 and 5 child elements. Next we have the leaf. A leaf shows the actual data, so it is similar to the actual data within the XML. So any XML element, including the root element, can have either no leaf or it can have exactly one leaf. Which means that the root can have some text data associated with it or any child can have text data associated with it. So, we saw the example of the emails XML document. So the root corresponds to the emails. The child element corresponds to the message. Then the message has child elements of to, from, the subject, the body and the attachment. And leaf are similar to the data. So, in to we have [email protected] In from, we have [email protected] In subject, we have How are you? In body, we have hi, let’s catch up sometime. and there is no data in the attachment. Now one other thing to remember is that XML is a W3C standard, so you can go to the W3.org website and there you can see the XML standard details. Now let us look at another way in which we can create the XML. So by using a text editor, the problem is we have to do a lot of typing. We have to type out the entire XML declaration etc. So an easier way to create XML documents is to use a Microsoft corporation’s program called XML Notepad 2007. So let us create this example XML document in XML Notepad. So what we need to do is just insert an element and I am going to call it emails. Now emails has a child element, which is the message. So Insert>Element>Child. And I can give message. So message does not have any text. Now using the shortcut of Ctrl + Insert, I can create a child, to and it allows us to type in what is the data. So [email protected] Then in message, we can have another child element which is from and this is [email protected] Then we can have the subject also. A subject How are you? and like that we can put the body and the attachment also, so once you have typed in all your tags and you have typed in the data. You can save it as an XML file. So I am going to call it messages.xml and we can go to our folder and we can see that messages.xml file is created. So if we open it with a text editor, we can see that the file is created here and we can open this file with the browser also. And this is our file so basically using XML Notepad, we can create our XML document very very quickly. So that is all for part 1 of the xML series. In the next video, we’ll see the XML rules and how we can read XML document by writing the source code. So thank you very much for watching this video and see you in the next video. Thank you.

Tags: , , , , , , , , , , , , , , , , , , , , , , , ,


Leave a Reply

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