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

XML DOM

XML DOM stands for XML Document Object Model. DOM is designed to be an interface to a tree-based representation of an XML document. In DOM, every XML entity (element, content, comments) is represented by a node object, where a node is a vertex in the XML tree representation. Different programming and scripting languages (such as Javascript, Perl, Java, etc.) that support information management through an XML API, chiefly support the XML DOM API. We will describe the functionality of the DOM API organization and features by looking at a real-world example.

Consider, for example, a corporate aerospace giant that has a hierarchical employee structure: nodes represent employees, and connecting tree edges (lines) indicate immediate supervisor and subordinates.

A DOM Interface provides an API that can be invoked by the user's program, and it is used to recursively traverse the XML document tree. It has certain standard, flexible API methods such as:

firstSibling(,) lastSibling(,) nextSibling(,) parent(,) firstChild(,) lastChild(,) and nextChild() among others. We shall now see how these APIs can be used to navigate the XML document. As is apparent, this is a very general API: the programmer doesn't need to know beforehand the number of elements, nested elements, etc. in the XML document.

Consider in particular the node representing the employee whose title is "VP Research & Development." In this node context, the above-mentioned API would return the following results, by traversing the tree in downward or upward direction, depending on the requirement:

firstSibling(): "VP Operations"

lastSibling(): "VP Business & Management"

nextSibling(): "VP Business & Management"

parent(): "President & CEO"

firstChild(): "PE Research & technology"

lastChild(): "PE Deployment"

nextChild(): (Depends on context)

To illustrate these results, note that the DOM internally maintains a pointer or marker to determine which node of the XML DOM tree is the current one. All the above operations return values relative to the current position of the pointer.

The DOM model is an old model found in early literature describing the invention of XML and related standards. Modern day XML-parsing standards have moved ahead, but we are learning about the DOM since there is a very large code-base out there in terms of XML language libraries and client code. DOM, in its heyday, had a huge following and is still the most general API around.

Video Transcript

00:00Alright, so we last left off, we talked about the XML structure itself.
00:06We actually seen it in practice using e4x which is obviously the technology of today
00:10and really the coolest way of working with XML.
00:13Now unfortunately it's not the only way of working with XML and there are a few other
00:17ways that are more archaic but yet widely used and most languages are actually working
00:23in it in this way.
00:25So that's why we have to kind of learn it.
00:28One of the stuff that, well, although it's archaic and I know that some people are
00:32going to send me hate mail for calling it archaic, it's called XML Dome and if you
00:37worked with HTML and JavaScript it will be familiar.
00:40If you haven't but you have some basic understanding in coding and now you know
00:45JavaScript, now you know XML, we should be able to cover, you should have enough
00:50information to be able to follow what we're talking about.
00:53So first of all before we dig into actually understanding what XML Dome is,
00:58we're going to ask a few questions, we're going to ask what it is, we're going to ask
01:01how, when to use it and we're going to set expectations.
01:05So first off we're saying that what it is, it's an W3C protocol that describes
01:10methods and properties for traveling through XML and really that's a really
01:14great definition of it.
01:16Now basically contrary to e4x or an e4x we kind of didn't really travel through
01:22the XML as much as we just wrote down what we wanted and fetched and our
01:27programming application just retrieved that information without us needing to
01:31actually step inside and physically go through a tree.
01:35Contrary to that XML Dome is more of a really representing that tree but
01:41you're kind of like walking through that tree and we're going to see in a
01:43second what that actually means.
01:45Now when should you use it?
01:46Really you shouldn't use it if you have access to e4x because e4x is just so
01:50much easier and really smarter so really you're going to use it only when you
01:55don't have other solutions that are just better solutions that are available
01:58to you but like I said before there's a lot of languages that this is just
02:02their tool set or older applications that you just have to know this to be
02:06able to work with them.
02:07Now our expectations like we said you need to know some basic HTML, some
02:11JavaScript and have that XML background that we just did in this course.
02:16Now you don't have to have in-depth JavaScript and HTML knowledge but you
02:20have to understand enough in it to be able to follow and you'll be able to get
02:24it even without knowing it but to really be able to leverage this knowledge it
02:28would immensely help having a little bit of background in JavaScript or
02:32scripting language and a little bit more in HTML.
02:37Alright so we're done with that topic and let's jump in right into that tree.
02:40So we're saying that XML is really a tree after all and we're looking at this
02:44family tree and XML is not that far away from a family tree, the
02:48relationship especially when we're working inside of this XML dome there's
02:53this relationship where you have a parent that has children and the children
02:57have children and so on and so forth.
02:59So if we dig a little bit deeper into that obviously in XML every node
03:04represents that group of parents and from now on let's assume that it's a
03:07single parent so our XML really this couple is really one.
03:12Now this one couple had five children so before we do that so we take that
03:16couple that couple had five children if we want to represent it in an XML we
03:21could have said that we have one person so this is our one person because we're
03:24ignoring the fact that they're two and that one person actually is not empty it
03:29has information it contains inside of it five other children five of their
03:33children so there we go so we have a pet we have our parent supposedly and
03:38we have this representation of the tree and obviously we could have gone we
03:41could have dig deeper into it but for the sake of our example this all is
03:45all that we need now until now when we were working with e4x we kind of jumped
03:50around without really needing to grasp this too much but now that we are inside
03:57of this relationship where we're actually walking through the tree
04:00basically that means is that at any given point I could be inside of a
04:04certain point on the tree so for example let's say I'm inside of the
04:07parent right now now when I'm inside of the parent I could then access my
04:14children so there's when we're going to look deeper into this when we're
04:17actually going to implement this code and we're gonna look at it into the
04:21actual API of the XML dome which that's all it really is it just describes all
04:26these functions and methods so if I just grab my little paper sheet with
04:30all these names because memorizing them is not really the point it's about
04:33understanding really the their goal so if we're looking at this first of all
04:37if we look at the relationship wise if there's there's first of all in a
04:41function or a method that enables you to a property that enables you to access
04:46your children it's called child nodes very logical we have then also an away
04:51from from the parent to directly access its first child it's called first child
04:57we could access the last child and that's from the from the relationship of
05:01the parent now if I if I if for a second we walked inside and now we're
05:06inside of we're now part of this and don't worry about this configuration
05:10because actually when we're gonna do it it's gonna make even more sense but
05:13imagine that we're actually now focused our pointer our finger is pointed into
05:18this child so if we take our pointer we're focused in this child right now
05:22we are in this child now when we're in this child if really let's say it's
05:26this girl right here now we're in this child we still have ways to talk
05:30back to our parents and we still have ways to talk to our siblings and if
05:34we had our own children we would have our ways so as a child if I had other
05:38children if I this person has more children I could access that child nodes
05:43again I could ask to act as my first child my last child and I could also
05:49access my next sibling so my next sibling would be basically if my pointer
05:53is right here I could then ask for my next sibling and my next my next
05:59sibling will actually be the next item now if we move to the next sibling and
06:03now our reference point is that that sibling we could always revert back and
06:09reverting back would be using the prev sibling so it's very very logical here
06:14now there's a few other points of just accessing some some random information
06:18not random but general information about nodes now that we see these
06:21relationships there are a few different types of nodes there are nodes that
06:24are directly text nodes like we've seen them previously where we just had
06:28content directly inside of our XML obviously some nodes have names some
06:34don't so if this node is called person but if I'm actually well this is a good
06:39point to carry through everything is a node in XML everything is a node in XML
06:44dome so even the content of a node even if it's just plain text is a node
06:48now that no doesn't really have a name actually its name is is hash mark text
06:52but there's quite a lot of different elements and every element is called a
06:56node in XML dome now one more thing that we have to notice about before we
07:01actually could jump in and start playing with the code itself is this is
07:04one of the most confusing things in XML dome is that actually line
07:10breaks are considered a node as well so when we're talking about that they see
07:14every time we broke a line we might have thought that we just did this
07:18for representation reasons and that's true in e4x it completely ignores these
07:22line breaks but in our case when we're well not necessarily we could set it to
07:27ignore whites but it's not really relevant because we're not walking
07:31through a tree so when I'm saying if I was now if I just undo this and I take
07:35my cursor back to the top and we're right now inside of my person and I
07:40ask for the first child the first child is not going to be this person
07:43it's not going to be what we would expect it's actually going to be this
07:46break of the line so it's going to be just an empty text okay so that's
07:50where people get confused really the most often with with XML dome
07:55unfortunately it's not well it worked when it first came out it's not the best
08:00you know it just being honest e4x is just so much better it's so much more
08:05superior so much easier so much everything else you want as to any any
08:09other name you want to put on it but you have to know this you have to know
08:12this because a lot of technologies work off of this still a lot of programming
08:16languages don't get updated that quickly action script fortunately for us
08:20as an easier platform to update more quickly contrary to JavaScript which for
08:25it to update it would have to update throughout every single browser before
08:28we could actually use it unless there's a third-party library that implements
08:33the concept of it but it wouldn't really be able to do one-on-one exactly the
08:37definitions that were we learned on any for X just because it would have to
08:40change the syntax of the language to change the engine itself of JavaScript
08:43which has to happen on all browsers and we'd have to wait a few good years
08:48before we could use it because we wouldn't be able to support older
08:51browsers so we just have to know this okay so that this really covers all the
08:55topics I really wanted to talk about in this theoretical point now we're only
08:59going to focus really on the basics here of traveling through XML because
09:03after you get that foundation finding all the different ways of going back
09:07which is basically your parent node okay or basically you're saying if I'm
09:12in the child right now to go back to the parents is possible as well I go
09:17to my parent if I wanted to go to my parents parent I would say parent node
09:20parent node if I wanted to go to my first child first child that would go
09:24first child first child okay so your your traveling path is really even at a
09:29level if I wanted if I was here and I wanted to get to the third one I might
09:32have just said I'm gonna take that node dot next sibling dot next sibling
09:36done I just got to the third one instead of the second one okay so
09:39that's what we're gonna focus on in our next video we're gonna actually see
09:43this in implementation and JavaScript but what I want you to get out of this
09:47is unfortunately this is a technology that you need to know you're kind of
09:51learning to support history while e4x is kind of learning to support the future
09:56and you are futuristic when you're working with e4x even if you don't have
10:00that in JavaScript right now knowing it is so powerful and you could always
10:03practice it in flash until other languages update and and get this
10:10specification it's just such a great specification it's approved by the EMCA
10:14screw so it's gonna happen at some point Firefox is working on it already I
10:18think they have some sort of beta version with it running but unfortunately
10:22you know we're in late 2011 or in November and it's even if tomorrow
10:28morning all browser supported it's gonna take a few good years before you're
10:31gonna be ready to actually work with it when you're not in a compiled
10:35application such as a JavaScript application or a server side
10:38application where you control the language when you're doing this up on
10:42the server contrary when you're on the client side you depend on what the
10:45client has on their browser right so that covers our theoretical intro to XML dome
10:51and now it's time to actually implement it and like I said before our goal is not
10:56to make you the masters of XML dome but it's our goal is to make you so
10:59comfortable with the concept and I hope that just by this intro you already
11:03understand the concept of it and we'll just implement it and see how it
11:07actually works in action in JavaScript in our next video

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!