Tags

Activity Industry Sector
Arts, Media & Entertainment

Activity Originally Created By: Courtney Paulos

Lecture

Part of Lesson Plan: Flash Apps: Building an Embeddable Sound Player Part 3 of 5

Activity Overview / Details

Below are teacher lecture notes as well as a link to download the printable version of the notes for teacher and/or students...

XML Lecture

XML documents form a tree structure that starts at "the root" and branches to "the leaves".


An Example XML Document

XML documents use a self-describing and simple syntax:

<?xml version="1.0" encoding="utf-8"?>
<PLAYLIST>
     <SONG URL="audio/track1.mp3" TITLE="First Track" ARTIST="John Doe" />
     <SONG URL="audio/track2.mp3" TITLE="Second Track" ARTIST="Random Singer"/>
     <SONG URL="audio/track3.mp3" TITLE="Third Track" ARTIST="Super Generic Singer" />
</PLAYLIST>

The first line is the XML declaration. It defines the XML version (1.0) and the encoding used (utf-8= UCS Transformation Format — 8-bit. UTF- 8 is most common on the web).

The next line describes the root element of the document (like saying: "this document is a playlist"):

<PLAYLIST>

The next 3 lines show the child element of the root PLAYLIST element. The child element is SONG:

<SONG URL="audio/track1.mp3" TITLE="First Track" ARTIST="John Doe" />
<SONG URL="audio/track2.mp3" TITLE="Second Track" ARTIST="Random Singer"/>
<SONG URL="audio/track3.mp3" TITLE="Third Track" ARTIST="Super Generic Singer" />

And finally the last line defines the end of the root element:

</ PLAYLIST>

You can assume, from this example, that the XML document contains 3 songs, their associated titles and the artist of each.

Don't you agree that XML is pretty self-descriptive?


XML Tags are Case Sensitive

XML tags are case sensitive. The tag <Playlist> is different from the tag <PLAYLIST>.

All this mean sis that the opening and closing tags must be written with the same case:

1.       <PLAYLIST>This is incorrect</playlist>

2.      <Playlist>This is incorrect</playlist>

3.      <PLAYLIST>This is correct</PLAYLIST>

When it comes to the issue of case sensitive, why do you think #1 and #2 (above) are incorrect?

why do you think #3 (above) is correct?

XML Attribute Values Must be Quoted

XML elements can have attributes in name/value pairs .

In XML, the attribute values must always be quoted.

Study the two XML documents below. The first one is incorrect, the second is correct:

<PLAYLIST>

<SONG URL=audio/track1.mp3 TITLE=First Track ARTIST= John Doe />
<SONG URL=audio/track2.mp3 TITLE=Second Track ARTIST=Random Singer/>
<SONG URL=audio/track3.mp3 TITLE=Third Track ARTIST=Super Generic Singer />

</PLAYLIST>

 

<PLAYLIST>

<SONG URL="audio/track1.mp3" TITLE="First Track" ARTIST="John Doe" />
<SONG URL="audio/track2.mp3" TITLE="Second Track" ARTIST="Random Singer"/>
<SONG URL="audio/track3.mp3" TITLE="Third Track" ARTIST="Super Generic Singer" />

</PLAYLIST>

The error in the first document is that the URL, TITLE AND ARTIST attributes in the song element are not quoted.


What is an XML Element?

An XML element is everything from (including) the element's start tag to (including) the element's end tag.

An element can contain:

  • other elements
  • text
  • attributes
  • or a mix of all of the above...

<PLAYLIST>

<SONG URL="audio/track1.mp3" TITLE="First Track" ARTIST="John Doe" />
<SONG URL="audio/track2.mp3" TITLE="Second Track" ARTIST="Random Singer"/>
<SONG URL="audio/track3.mp3" TITLE="Third Track" ARTIST="Super Generic Singer" />

</PLAYLIST>

In the example above, <PLAYLIST> has element contents, because it contains another element … <SONG>

 <SONG> has 3 attributes

URL="audio/track1.mp3"

TITLE="First Track"

ARTIST="John Doe"


XML Attributes Must be Quoted

Attribute values must always be quoted. Either single or double quotes can be used. For a song, the song element can be written like this:

<SONG URL="audio/track1.mp3" TITLE="First Track" ARTIST="John Doe" />

or like this:

<SONG URL= ‘audio/track1.mp3’ TITLE=‘First Track’ ARTIST=‘John Doe’ />


XML Elements vs. Attributes

Take a look at these examples:

<person sex="female">
      <firstname>Anna</firstname>
      <lastname>Smith</lastname>
</person>

 

<person>
      <sex>female</sex>
      <firstname>Anna</firstname>
      <lastname>Smith</lastname>
</person>

In the first example sex is an attribute. In the last, sex is an element. Both examples provide the same information.

There are no rules about when to use attributes or when to use elements.

Materials / Resource