02Geek HTML5 and JavaScript, TypeScript, React, Flash, ActionScript online School
Previous VideoNext Video

XML Validation

"Life is like a box of chocolates. You never know what you'll find inside!"

(A famous line from the brilliant Oscar-winning movie "Forrest Gump.")

Yes, life is full of surprises and uncertainties, and this is the reason life has a charm to it. But programmers and software clients don't like surprises… especially the bad kind! And the same holds for information that is processed by programs.

Software programs can work only when they can find structure and patterns in information, and XML was invented to help with that. So, validation of XML documents is also important. In particular, the syntactic structure of XML documents should be the same as expected by a computer program calling it. It is then considered "well-formed." As we saw in an earlier session, correct matching of start and end tags for each element, as well as correct nesting of elements (without overlap) are the main issues here.

Consider, for example, a brothel owner who has to open a box of condoms, check that the contents are as expected – none are damaged or missing, before selling it in his brothel. If we were to describe this scenario in terms of a computer program that is given an XML document, we would say:

  1. The brothel owner : the browser's XML parser application
  2. The box of condoms: The XML document. Each type of rubber is an element category in the XML document.
  3. The brothel: the browser that accesses this XML doc stored on the web server, and displays the contained information on the browser interface.

First, let us define a well-formed XML document that represents the box of condoms:

<?xml version="1.0" encoding="UTF-8" ?>

<box>

<boxOfRubbers>

<rubber id="0">

<rubberName>Ribbed</rubberName> <quantity>4</quantity>

</rubber>

<rubber id="1">

<rubberName>Glow in the Dark</rubberName>

<quantity>3</quantity>

</rubber>

<rubber id="2">

<rubberName>Chocolate Flavored</rubberName>

<quantity>4</quantity>

</rubber>

</boxOfRubbers>

Now, when the brothel owner (browser's XML parser) opens (parses/scans and separates) the box of rubbers (XML document,) he might find one or more of the following inside:

  1. A few rubbers have melted (similar to no proper nesting of element start- and end- tags.) In other words, our xml is not well formed.
  2. A few items, (for example all the ribbed rubbers – say THAT 3 times very fast!) are missing. Could have been stolen by a picky hooker. Again, not well formed.
  3. All types and quantities of condoms advertised on the box cover are intact. In this case, the XML doc is well formed.

If the brothel owner discovers that the box of rubbers is incomplete (not well formed,) then they do not sell them to clients. Instead, they return the box to the store where they bought them, along with some harsh words, and demand a refund. Similarly, if the browser's XML parser discovers that the document is not well formed, it displays an error on the browser window. The creator of the document must then correct the syntactic structure.

Hope we've conveyed to you the importance of having a valid XML document for well-behaved, consistent software applications, so that they can earn you good money!

Video Transcript

00:00In an ideal world, we're always right.
00:03You're always right.
00:04I'm always right.
00:06Unfortunately, as programmers,
00:08we have to actually confess to the fact
00:09that most of the time we're wrong and we make mistakes.
00:13And that includes when we're working in XML.
00:15So we're gonna cover two topics here
00:16before we move on to our second chapter,
00:20which is gonna be the actual development
00:21of applications using XML.
00:25Our next phase in our application,
00:27what we're going to do is we're gonna see,
00:28actually, we're gonna create our base XML file
00:31from start to scratch,
00:32which is gonna be used throughout our different languages
00:35as we're gonna practice them.
00:37After we create that and while we're creating it,
00:39we're gonna validate it and make sure
00:41that our XML is built correctly.
00:43Now I'm gonna be using Dreamweaver CS 5.5
00:47to build my XML file out.
00:49And I'm gonna be using Mozilla Firefox,
00:51which could be downloaded on almost any operating system
00:55to validate my files.
00:56So let's dig in and start building
00:59our XML file.
01:00So I'm just gonna end Dreamweaver,
01:01I'm just gonna select an XML file.
01:03As you can see in the first line,
01:06the XML is already set by default
01:08to have all these different properties.
01:09One is that our XML version is 1.0
01:13and that our encoding is UTF-8.
01:15Now if you're working in a multilingual application,
01:18an application that has a lot more characters,
01:20if it's Japanese, if it's different languages,
01:22I would just switch this to 16.
01:24But most chances are if you're just working
01:26in English, French, and just our basic languages
01:29that are more European-based,
01:31then you have nothing to worry about.
01:33All right, instead of dealing with those irregularities
01:36that I'm sure you'll deal with and scratch your head in,
01:38and I'm sure we'll be meeting up
01:40in our community forums
01:41or in different public forums on the web.
01:44The first step I'm gonna do, like we said,
01:46we could only have one base node.
01:48And our base node, we're gonna call,
01:50not only we only could have one,
01:51but we have to have one, we're gonna call it school.
01:54Now what's really nice in Dreamweaver
01:57is that Dreamweaver CS5.5,
01:59as soon as I select to close it,
02:00it will automatically recognize it.
02:02Now because I've been playing around with the nodes,
02:04every node that I created now will exist in its memory.
02:07So all these different nodes that I was playing around with
02:09are already in its memory automatically.
02:11I didn't have to do anything.
02:13All right, so I already have now my school node.
02:16My next node that I wanna create
02:17is I wanna create student nodes.
02:20And I already have them already pre-typed
02:22just because of the fact that I already played with it
02:25a little bit earlier.
02:28Now I created my student node,
02:29but I really wanna give a name to my student.
02:32So every student is gonna have a name.
02:34And again, we wanna make sure
02:36that we're putting the spaces correctly
02:37and we wanna make sure that we're opening and closing
02:39our double quotation mark.
02:43All right, now next step, every student will have a score.
02:46Now, contrary to before,
02:47where we just put every topic as a separate node,
02:50we're gonna do it in a different way this time around.
02:53So this time around, I wanna create a score or an exam.
02:56So I'm gonna create an exam node
02:58and the exams node will have a type
03:00which would then tell me what type of exam it is.
03:03So let's say the first type is going to be math
03:06and I'm gonna close that exam.
03:08And in the exam, I'm just gonna give a score,
03:10let's say of 80.
03:11Now, because we know how to create this already,
03:13I could just copy paste,
03:14which is probably one of the most convenient things
03:16we're doing with XML and then changing our value.
03:19So we just put here three different exams.
03:22We don't want all of them to be the type of math.
03:24So we'll put here one that will be English
03:29and we could put here one that will be Spanish.
03:33All right, now let's make a few more students.
03:36So I'm gonna copy our first student
03:38and just paste it a couple of times.
03:40So I have a nice amount of students.
03:42We have right now four students.
03:43Let's change our name.
03:44So we have your Ben, let's have a John,
03:47let's have a Mary.
03:49And just so we're equal, two boys and two girls.
03:53So let's have your Linda, intent.
03:57All right, so we have your Ben, John, Mary and Linda.
04:00Now we don't want all to have the same score.
04:02So let's change around the scores.
04:04Let's say one will have 90, one has 50
04:07and let's say 55, 33, 90, 12, 99.
04:18One brilliant person, which is Mary will get 100.
04:21Linda's really bad with math, she got a five.
04:25She's created English actually, 100 and Spanish,
04:29I don't know, so let's give her 50.
04:31All right, so that really covers it.
04:34We have our XML, it's ready now to be used and tested out
04:37and actually right now it's a perfect XML.
04:40So I'm gonna save out this XML file.
04:50So I selected my file and I'm saving it.
04:55I'm gonna replace our previous file that was there.
04:57Our next step now that we actually have a file
04:59and it's working, we wanna actually really make sure
05:02that our file validates before we actually go
05:04and program with it.
05:06Because if something is broken with it, we might get lost.
05:09Now the easiest way to test it out,
05:11especially if you don't have Dreamweaver
05:13and you just have a basic text editor
05:15is just throw the file directly into your Firefox
05:18and your Firefox will then automatically
05:20just validate technically
05:22that the file format is acceptable.
05:24Now the way to do it,
05:25I'm just gonna minimize my Firefox for a second,
05:29find my file.
05:31And you can see that we're already in great shape
05:35and I'm just gonna put our file right here next to it.
05:42There we go.
05:43So first of all, we have our original file on one end
05:46and we have our XML document.
05:47We have our XML document outputting on the other end
05:50and we can see that although we don't have
05:52any style information, that's fine.
05:54We didn't plan to have style information
05:56but we can see our tree structure.
05:58We could minimize, we could maximize our XML
06:01and we could see that we don't have any errors or any.
06:04Now if we would leave an open tab,
06:06let's say we forgot to close our exam or had a typo,
06:09maybe we forgot to put an X there and we saved the file
06:13and we didn't have Dreamweaver to help us type in
06:16and we would refresh our application,
06:18we would then get an error message.
06:20It's not very descriptive but in Firefox
06:23it's telling us in line number four,
06:25that's where we are, column 25.
06:27So we have to count 25 or we could just look at the picture
06:30and see that the error is happening right here
06:32and if we look at it, we could then say,
06:34oh, there we go, we forgot to put in our exam,
06:37refresh our application and we're gonna see that
06:40through the browser, we could see the XML
06:42just as we intended it to look like.
06:46Now one thing I already could see here
06:49is one thing is like, and by the way,
06:51don't worry about this weird copy
06:53or I think it's just part of my operating system
06:55but as long as your text is outputting
06:58and showing itself correctly, then you're fine.
07:01Now, by the way, what might be happening here,
07:04I might have your extra space, no, I don't know.
07:07All right, so, but just ignore that
07:09because it's not really relevant.
07:10As long as your XML is coming clear,
07:12you know you're okay.
07:14Computers sometimes do weird things,
07:16I don't always know why they do them
07:18but in this case, that's one of those weird things
07:20and I don't really know why that FF is appearing there.
07:23If you do then, please let me know.
07:26And that's it, so we have our basic XML sample
07:29and from now on, we're gonna base our work
07:31off of our basic XML that we've just created
07:34and from now on, any time you build an XML, easy enough,
07:37just drop it into your Firefox
07:39and then try to see if you have any issues with your code.
07:43Now obviously, if you're working with Dreamweaver CS 5.5,
07:46we'll automatically remember the nodes that you're using
07:49which will make your life easier
07:50especially because we're not building it,
07:52with schemas that's out of our topic,
07:56we're really taking our XML skills
07:58into getting this basic knowledge of working with XML
08:01and then seeing how do we implement them
08:03in different programming languages
08:05instead of using our XML as a main real language.
08:09We're just leaving it open-ended,
08:10enabling you to type whatever you want
08:12like most people do in most projects.

Ready to Level Up Your Skills?

Join thousands of learners on 02GEEK and start your journey to becoming a coding expert today!

Enroll Now for Free!