Introduction to XML in Flash
XML can have another type of data structure
called attributes. Attributes are additional
(generally simple) values which can be included
in element nodes. They consist of a name (whose
naming restrictions pretty much follow that
of elements) and a quoted value included within
the opening tag of an element. Unlike, elements,
however, they have no child nodes or hierarchies
of their own.
So far we haven't seen any attributes as they
are optional parameters. Though, should you
so desire, you can have as many or few as you
desire for each element in your XML. Here is
a previous example that now uses attributes:
- <letter to="Sandy"
- I love you!
This takes two text nodes which were before
in elements and places them as attribute values
in the letter element. Since the rest of the
letter is pretty much assumed to be its body,
we can leave the "I love you!" text
node directly within the letter element as a
child. Such text, since it has the potential
to be long and fairly complicated, would probably
not be proper as an attribute anyway.
It is important to understand that attributes
aren't meant to contain complex information.
They must exist on a single line, in a single
element and have a value defined in quotes.
Since attribute values are defined in quotes
they themselves cannot contain quotes - at least
not the same kind of quotes used in their definition.
Attributes can be defined with either single
or double quotes so you can have single quotes
in an attribute defined with double and vise
versa, but you can't have double within an attribute
defined with double. Attempting to escape quotes
with a backslash (\") will not work either.
And this not only applies to quotes, but also
other characters you may wish to include such
as tab (\t) or new line (\n). The attribute
will treat each as plain text.
Ok, you got me! The above is not entirely
accurate and all is not entirely lost
if such aspects are required in your attributes.
Like elements, attributes can use the aforementioned
character references as well as others in the
ISO 8859-1 (Latin-1) character set. This also
includes the original 7-BIT ASCII standard.
So yes, quotes can be added and yes, tabs and
new line characters too. Want to know what they
out more here.
Regardless, my position still remains; attributes
should be kept simple. They are used to provide
properties for elements, not relay your life
story. The simpler the better as too many attributes
or too much information within attributes can
create unreadable XML, and that's not what we're
after here, is it?
Ok, now take a look at this XML:
- <a href="http://www.senocular.com">senocular.com</a>
Oh wait, its an anchor tag in HTML. No, its
XML I say! You got yourself the element a with
an attribute href whose value is "http://www.senocular.com"
and a text node child of the a element whose
value is senocular.com. Notice the similarities.
XML isn't so bad. If you've been working with
HTML then you've practically been working in
XML. Just remember to keep those line break
- <br />
(Now you're making XHTML!)