Introduction to XML in Flash
       by senocular

Evaluating An Example of XML
Let's look at a small example of XML and check it for errors... if there are any.

<thursday>
<Meeting time="10:00">
This meeting was boring. >_< I made paper airplanes with a memo I received earlier.
</Meeting>
<lunch time="1:30">
Peter came over to talk to me. He seems depressed.
<served maincourse="Steak"
side1="Apple"
side2="Mustard"
drink="Tang" />
<spent mealcard="0" cash="12.00" />
Peter came over to talk to me. He seems depressed.
</lunch>
<Meeting time="3:00" time="5:00">
These meetings were even more boring than the first.
 
I slept straight through both.
</meeting>
<18holes_of_golf />
</thursday>

From looking at the root element, you can tell that this XML document contains information about thursday. The first child element of thursday is a Meeting element that contains an attribute time. This attribute's value is10:00. So far so good.

Within that Meeting element is a text node. It has text referencing the meeting, how boring it was, and has one of those silly ASCII faces in there. Uh oh, that marks a problem - the use of < and > within a text node. They need to be replaced with their respective character references.

<Meeting time="10:00">
This meeting was boring. &gt;_&lt; I made paper airplanes with a memo I received earlier.
</Meeting>

Other than that, this Meeting element looks fine.

Next up is the lunch element. Notice that it contains both a text and element nodes. No problem there. A text node can coexist with other elements within any given parent node. The only problem you might have is with the second text node (below spent). Why is it exactly the same as the first? Well, there's nothing technically wrong with it. I think this person was just very adamant about that particular thought. One thing to get from this is that a text node starts at an element tag (an opening or closing) and ends where the next one picks up (which too can be an opening or closing tag). So, despite the similarities of the two text nodes in the Meeting node here, they are distinctly separate because of the elements between them.

You may have noticed that the served element within lunch is a little funky looking. There's nothing wrong with that either. White space within elements is negligible so long as you don't divide attribute values with new lines.

Following the lunch element you can see another Meeting element. It has the same name as another element, but there's nothing wrong with that. It also has two attributes, time and... time. Red flag! That won't work. Attributes can't have the same name as other attributes in the same element. Better change one of those. Lets use time and duration instead of two time elements.

<Meeting time="3:00" duration="2:00">

Within meeting is a text node. It is a single text node consisting of two divided lines. It's content is innocent, so there it's not a problem. The following tag, however, the closing tag of the Meeting element, is. What's wrong? Its name is all lowercase, whereas the opening tag started with a capital M. Better fix that bad boy and make sure the closing tag also has a capital M.

<Meeting time="3:00" time="5:00">
These meetings were even more boring than the first.
 
I slept straight through both.
</Meeting>

Last but not least it looks like Thursday was wrapped up with a little golf (this must have been the agenda for the boss?). Again, small naming problem. Elements can't have names starting with numbers. We'll have to switch that around a little bit. How about putting the 18 in an attribute? Never know when you'll play less than 18.

<golf holes="18" />

Though we've gotten rid of the underscores (_) they were not a problem.

That brings us back to the closing tag of the single root element which, itself, is just fine. With our corrections, the XML now looks like the following.

<thursday>
<Meeting time="10:00">
This meeting was boring. &gt;_&lt; I made paper airplanes with a memo I received earlier.
</Meeting>
<lunch time="1:30">
Peter came over to talk to me. He seems depressed.
<served maincourse="Steak"
side1="Apple"
side2="Mustard"
drink="Tang" />
<spent mealcard="0" cash="12.00" />
Peter came over to talk to me. He seems depressed.
</lunch>
<Meeting time="3:00" duration="2:00">
These meetings were even more boring than the first.
I slept straight through both.
</Meeting>
<golf holes="18" />
</thursday>

Looks good! If you were able to catch all those mistakes then you should be on your way to being able to make your own valid XML!


 




SUPPORTERS:

kirupa.com's fast and reliable hosting provided by Media Temple.