LOADING ...

XML coding tutorial for beginners - Part 2

38K+ views   |   271 likes   |   11 dislikes   |  
Jan 24, 2015

Thumbs

XML coding tutorial for beginners - Part 2
XML coding tutorial for beginners - Part 2 thumb XML coding tutorial for beginners - Part 2 thumb XML coding tutorial for beginners - Part 2 thumb

Transcription

  • Hello, and welcome to the XML tutorial for beginners. This is Part 2.
  • In this video we are going to learn the XML rules and how we can read XML by writing a simple program.
  • Now XML is more strict than HTML.
  • So there are certain rules that we need to follow when we create our XML document.
  • If you don't follow the rules, your XML will be rejected. So let us get started.
  • The first rule is regarding the XML declaration.
  • So
  • every XML document starts with an XML declaration.
  • Now here in this example, we have the root element, but there is no XML declaration.
  • So this is incorrect. The correct way to write the XML document is to first of all put the XML declaration.
  • So every XML document starts with less than question mark XML
  • And then we have to give the version of the XML and we can give the optional encoding also.
  • And after the XML declaration, we have the root element. The root element here is emails.
  • This is the emails opening tag and this is the emails closing tag.
  • The next XML rule is regarding the root element. The XML document should have exactly one root element.
  • Now what is the problem with this example?
  • It does have the XML declaration at the beginning and
  • then it has a root element, so
  • we have the opening tag of the root element then we have the closing tag of the root element.
  • Now, the problem is that everything else within this XML document should be within the root element. This is not the case.
  • After the root element ends, we have another opening tag and closing tag so that is the problem with this XML document.
  • The next XML rule is regarding the opening and the closing tag. So each XML element must have an opening tag and a closing tag.
  • So, let us see an example.
  • So, we have
  • some XML element where member is the opening tag. Then we have the role opening tag and the role is father.
  • Then we have the member closing tag.
  • Now in XML, the elements have to be correctly nested.
  • So, the role should be totally within the member XML element.
  • Why? Because role is a child element of the
  • member
  • XML element.
  • So, this is not the case. That is why this is incorrect. The correct way to represent this XML is to have the member
  • opening tag, then the role opening tag.
  • Then we can have the value, then the role closing tag and the member closing tag.
  • It is also possible that we do not have the value in the XML element.
  • So, in that case we can just leave it as blank.
  • So, there is a shortcut available in XML and that is with a self-closing tag.
  • We don't have to repeat role opening tag and role closing tag. We can just put role/
  • Now let us see the guidelines for naming the tags.
  • Few guidelines for naming the XML tags are as follows.
  • First of all,
  • keep in mind that the XML tags are named by the author. So if you are the one who is creating the XML document,
  • you have to make sure that the tags are meaningful. And they follow certain rules.
  • Because XML documents can be read by human beings and they can be read by software applications both.
  • The first guideline is that the tags should be meaningful.
  • So, an example of a meaningful tag name is name which indicates the name of a person or a thing.
  • But if we put first_tag,
  • that is not meaningful.
  • It just indicates that if this is the first tag.
  • Or N may stand for name. It may stand for notes.
  • It can stand for something else also, so always have meaningful tags, so whether people can understand it and
  • software application can also understand it. The developer who is writing the software application should be able to understand what data is there.
  • The next guideline for naming tags is that the tags should not be too long.
  • So, this is a good tag, age.
  • But if we write a very long tag like the_current_age_of_member, so this is not readable.
  • It is going to take too long for a human being to read it also.
  • And when the developer writes the code to read the XML, it's going to take a longer time so have
  • tags that are
  • meaningful, but not too long.
  • Now, this is a very important guideline.
  • XML tags are case-sensitive so the opening and the closing tags must have the same case.
  • So the correct
  • opening tag is occupation and we have the correct
  • closing tag which is slash occupation, but if you notice this example, here the O is capitalized in the
  • opening tag, which is incorrect because XML tags are case-sensitive.
  • We should not start any XML element name with XML.
  • Capital X or any other variation.
  • This is a guideline because sometimes the application may get confused whether a particular element is an
  • element or it's some kind of a declaration. So do not include the
  • characters xml in your tag names.
  • The next XML rule is regarding the correct nesting so here we have an XML . So we have the member opening tag and
  • we have the member closing tag here.
  • And if you see the name opening tag and the name closing tag overlaps the member XML element. This is not allowed.
  • Why? Because name is a child element of the member element and it should be totally within the parent element.
  • The correct way to write this XML is to have the member opening tag
  • at the beginning and have the member closing tag at the end and within this we can give the name
  • opening tag, then the value and the name closing tag.
  • Just like HTML, we can have comments in XML, and we can have comments after the XML declaration.
  • Why after? Because XML declaration is a place, which
  • indicates to the software application or the browser that what follows is an XML document so comments are allowed only after the XML declaration.
  • So here we have the root element,
  • which is familyMembers so familyMembers opening tag is here and we have the familyMembers closing tag here. and
  • In between, we have the
  • XML comment. So XML comment starts with less than
  • exclamation -- and then we can give any text.
  • And it ends with --greater than.
  • The purpose of XML comments is to make the XML more readable. You can write your own comments
  • to give additional information, but the comments are going to be ignored by the XML parser or the XML processor.
  • Now, we come to the important rules for naming the tags.
  • So XML tag names can contain letters, numbers or other characters also. So, for example, we can have
  • letters, and we can have digits also or we can have like primary_occupation. We can include other characters also.
  • But XML tag names cannot start with a digit or a hyphen or a dot.
  • And also XML tag names - they cannot contain spaces so let us look at an example.
  • So here we have an XML document. So in the beginning you can see we have the XML declaration.
  • Then we have the comment and
  • the root element is the employees. Now employees has
  • exactly
  • one child element which is called the employee. And with an employee, we have the child elements like
  • the first_name and the
  • last_name and the hobby etc.
  • So the first thing is that can we put a digit in the beginning.
  • So let us say I want to give the opening tag name as 1first_name and I give the
  • closing tag as
  • first_name.
  • So if we open it in the browser,
  • so you see that we get an XML parsing error. The browser's complains that this XML is not well formed.
  • Well formed XML means that it follows all the XML rules.
  • So it is rejected.
  • So
  • same thing we can give for a
  • - sign also or a dot also or let us try to put spaces here.
  • So I have given the opening tag and the closing tag. So obviously, the opening tag and the closing tag have to be the same.
  • But we have included a space here.
  • So let us try with this.
  • So again, the browser rejects it. It says this is an XML parsing error. It's not well formed.
  • But we can use other characters like underscore to separate out the words.
  • Whatever scheme you use, you should follow it.
  • You can use underscore to separate the words or you can use camelCase notation or you can use
  • Proper Case notation.
  • So now, if we open it then, this XML is correct.
  • Now, let us go back. XML tags can also
  • not contain these characters - exclamation sign, double quotes etc.
  • Now let us look at an XML rule for the XML attributes.
  • So, XML elements can have attributes. What are attributes? Attributes give additional data about the XML element.
  • So, let us look at an example. So this is an XML element.
  • The tag name is hobby. So we have the hobby
  • opening tag here, and we have the hobby closing tag here, and we have the value as Gardening.
  • But let's say, we want to give some additional information.
  • What is the schedule of the hobby? So, if it is daily, weekly, monthly, anything? So we can include an attribute.
  • So, the attribute name is schedule, and its value is weekly.
  • Whatever value
  • we give for the attribute, that has to be enclosed within either both
  • double quotes at the beginning and in the end OR single quotes at the beginning and at the end.
  • So we have already seen that attribute text should be within double quotes, or single quotes.
  • So, we have seen the example of using double quotes here.
  • So we can have
  • let's say, an XML element of name and here the attribute is last_name.
  • And we can have the attribute text in single quotes also - one single quote at the beginning and one single quote at the end.
  • Sometimes it is possible that the attribute text or the attribute value includes a
  • single quote or a double quote then we have to use both
  • double quotes and single quotes.
  • So an example is let us say the name XML element has a
  • last_name
  • attribute and the value of that attribute is O'Rielly
  • But after O, there is an apostrophe.
  • So this is allowed because we are including the attribute value within the double quotes at the beginning and the end.
  • In case you have a double quote within the attribute text, you can enclose the entire
  • attribute text by single quotes.
  • Now, let us come to an important topic in XML and that is for character entity references. So there are some
  • characters which have special meanings in XML.
  • So these characters are the double quote, ampersand sign, apostrophe, less than and greater-than.
  • So, let us say we want to include quote.
  • So, instead of just writing the
  • double quote, we have to give the entity reference, which is
  • ampersand followed by some symbols. In the case of a double quote, it is quot and
  • ending with a semicolon.
  • So here for example, we want to include
  • The XML value, surfing within double quotes, so we have to give one double quote
  • entity reference in the beginning and one at the end.
  • Same thing for the ampersand sign. We cannot just have ampersand as a part of the XML value.
  • So, we have to use this character entity reference for that.
  • Same thing for the apostrophe symbol also.
  • And same thing for less than sign and greater than sign. This is similar to the HTML entity references.
  • So let us say that your XML element has a value and you want to put this
  • so
  • you want to put less than he greater than double quotes ampersand
  • something you know, so you cannot simply put this
  • text as a XML value because it is going to give an error. Why?
  • Because when it sees the less than sign, less than sign has a special meaning in XML.
  • So it is going to assume that this is some kind of a tag and it is going to interpret it in that way but
  • using character entity references,
  • we can have this
  • in the XML value.
  • So
  • here what all we need to do is replace each of these characters with the character entity reference.
  • So for example for less than, we have to put <
  • For greater than, we can put
  • this thing.
  • So
  • the result you can see over here. So you see exactly He is enclosed by the angle brackets and
  • surfing and other stuff is
  • within double quotes and we can put apostrophe ampersand all these things by using the character entity reference.
  • So in XML, There is a concept of well-formedness.
  • Well, what a
  • well formed XML document means is that your XML document follows the XML rules.
  • So there is no rule violation in your XML. So when your XML document is well-formed, only then you should
  • provide it to another person or you provide to a software application for reading it.
  • Now let us
  • go to how we can write the program for reading the XML. We have already seen
  • XML Notepad 2007
  • that we can use to
  • write the XML document
  • Now, let us see how we can read the XML document and the XML document that we are going to read is this employee
  • XML document.
  • So I'm going to
  • write this
  • program within HTML, so first of all I put the HTML
  • opening and closing tags because this is an HTML document.
  • Then I am going to have the head section and the body section and then I am going to include a script
  • within the head section. So I am going to have some Javascript code that is going to read this XML document.
  • So, I
  • create
  • some variables. So first of all, I have the xmlObject.
  • xmlObject, I am going to put the XML HTTPrequest so how exactly we put it, we will see.
  • Then we have the docObject. This is going to contain the
  • XML representation, then we have a couple of variables. We want to read the name and the date of joining of the employee.
  • So the first line of code that we need to write is xmlObject = new XMLHttpRequest();
  • Then the important line of code is: we call the open method on the xmlObject.
  • So the first argument of this method is what type of open method we want to use? Whether we want to use get or post?
  • So get is faster. That is why I have used it.
  • The second argument of this method is the location of the XML file, so in the same folder,
  • I have the employee.xml file.
  • So I am going to open that and the last
  • argument of this method is what is the asynchronous?
  • Whether it is asynchronous or synchronous?
  • So false means it is a synchronous opening. So we can use false for smaller XML documents.
  • And we can use true, if the XML document is large and there are other factors also.
  • Then we call the send method on the xmlObject.
  • So this sends a request to the server and we actually get the XML.
  • Now
  • we get the responseXML. responseXML returns the XML representation and this we store in the
  • docObject variable. Now further on in this Javascript, we are going to be using the docObject only.
  • So these are like some
  • standard lines of code that we can use to open any XML file and have its
  • representation in some variable.
  • So the first thing we do is we got put the method of getElementsByTagName, so
  • we can call it on the
  • responseXML that is returned to us and there we search for first_name
  • So the very first name is represented by 0 because it's an array
  • starting with zero and
  • the very first child node and the node value.
  • So that we put in the
  • HTML webpage. Name = name variable, whatever value we got for the
  • first_name
  • Then same thing we can do for
  • the date of joining also, so if you notice
  • the first_name is directly under employee, but the date_of_joining is a parent
  • XML element because it has three children - day, month and year.
  • So we are expecting 12 January 2014 to come here.
  • So, we can again use the getElementsByTagName and this time we are interested in finding the day.
  • Then
  • we concatenate that by putting dateOfJoining + getElementsByTagName of month and
  • same thing we do. dateOfJoining + getElementsByTagName over here.
  • So now dateOfJoining has a complete string available in it.
  • So we write
  • document.write dateOfJoining is this
  • So let us execute this.
  • And here we can see that, we got name = John so it has gone into
  • the XML file and
  • extracted the
  • first_name XML element.
  • The date of joining = it has concatenated three elements values,
  • day, month and year.
  • So that is how we can
  • read the XML
  • document.
  • Now let us say that we
  • enhance this file. Instead of having a single employee, I have put three employees here.
  • So here in employee, we have an attribute id and the value is 1, the second employee
  • id value is 100, the third
  • employee id attribute value is 101.
  • So let us see how we can get data from all the elements of the XML.
  • So, for that the code is very similar. So in the beginning you
  • remember that we put new XMLHttpRequest();
  • Then we call the open method. Then we call the send method. Then we got
  • responseXML so here the only difference from the last program is that
  • we call the method getElementsByTagName("employee") and
  • to put that in the
  • employeesArray.
  • Now employeesArray instead of containing data about
  • just one employee, it's going to contain data of all the employees, whether there are three employees or 100 employees.
  • Then,
  • we run a for loop. And for counter is equal to zero
  • counter less than employeesArray.length
  • So if there are going to be three elements in the array,
  • So employeesArray.length is going to be 3 so we are running a for loop from 0
  • to 2.
  • So it is going to run three times for the counter value
  • value of 0, then second time it is going to run for counter value of 1 and
  • the last time, it is going to run for counter value of 2. And every time we increment the counter by ++.
  • This is a standard Javascript for loop.
  • Next, what we do is the same thing as the last program getElementsByTagName. Put first_name
  • but instead of giving the value 0 as
  • in the last program, here we get the counter because we are not interested in always getting the data for the first employee.
  • We want to get the data of all the employees. The first time counter is going to be 0.
  • So it is going to get the name of the first employee. The second time the loop runs,
  • the counter is going to be 1 so it is going to get the name of the second employee and so on.
  • So we write that value.
  • Then the difference from the last program is that here we are trying to get an attribute value.
  • Because if you look at the department.
  • Now, department
  • XML element has an attribute called role.
  • So where this is Manager for John Devon employee and
  • The name of the Department is Sales.
  • If we look at data for the second employee, the role is Developer, so Irene Bishop
  • is a
  • Developer in Engineering.
  • So how we get the attribute is by using the getAttribute method.
  • So we specify the name of the attribute which is role and
  • getting the department is the same as getting the name. Using getElementsByTagName, we can get the department. So let us
  • run this now and
  • see.
  • So, we are getting name is John and position is Manager in Sales.
  • So this data is
  • coming from both the
  • department value and the Attribute value.
  • So Sales is the department and the attribute is Manager.
  • So Engineering is the department and role is Developer. Engineering is the department and role is the QA.
  • So like this, we can see that
  • using a simple program we can read the XML. Provided we know the structure of the XML. We have a sample XML document
  • we can use that to write our
  • program. So here, I have used Javascript, but you can use any
  • programming language to read the XML document, whether you use Java, C++ or C#, you can do
  • this.
  • So thank you for attending this video and
  • thanks once again.

Download subtitle

Description

Java script code to read XML values shown and demonstrated in this video. Click Cc button to see subtitles in English. See part 1 video at /watch?v=O87VaZAN7As

Next, by Software and Testing Training
JSON Tutorial (no programming knowledge needed) : /watch?v=IqI5yiMvJ6A

Test Automation : https://www.youtube.com/playlist?list=PLc3SzDYhhiGXVcy8EcrTSfwsC-v8EUZvg

Database Testing : https://www.youtube.com/playlist?list=PLc3SzDYhhiGVVb76aFOH9AcIMNAW-JuXE

Programming Concepts : https://www.youtube.com/playlist?list=PLc3SzDYhhiGVqrvE28rlJR75asbujHoUL

Software Testing Tutorials : https://www.youtube.com/playlist?list=PLc3SzDYhhiGWuMK03uYO-UENAx0s2woWM

Website : http://inderpsingh.blogspot.com/
Twitter : https://twitter.com/inder_p_singh

Trending videos