Introduction to XML in Flash
by senocular
Element Nodes
Elements are the actual XML tags (or folders).
Each letter, to, from and body are all XML elements.
- <letter>
- <to>Sandy</to>
- <from>Peter</from>
- <body>I
love you!</body>
- </letter>
The letter element represents the XML document
root element. There should only be one
root element tag for every XML document (though,
actually, Flash specifically could care less).
All other elements should be placed within this
root element to define the entire contents of
the document.
When one node is in another node, it is said
to be a child of that node. A node
with children is considered to be the parent
of its children - almost like we're dealing
with a family tree. The letter element has 3
children, to, from and body. The parent node
of each of those three is letter.
All element nodes in XML must have a beginning
and an end - an opening and closing tag. Elements
with no content within an opening or closing
tag, or having no children, like <to></to>
for example, are called "empty elements"
and can be alternatively written as <ELEMENTNAME
/> or in the case of to, <to />, where
you have one tag basically closing itself. Having
just <to> without any closing would be
incorrect. The following is the same example
from before, just without any text within the
to element making it empty.
- <letter>
- <to />
- <from>Peter</from>
- <body>I
love you!</body>
- </letter>
Apparently Peter just got dumped.
When naming elements, there are certain rules
you should follow. Element names must contain
valid only characters. These include letters,
numbers, underscores (_), hyphens (-), periods
(.), and colons (:). Colons are typically only
used as a namespace delimiter, a topic which
won't be covered here. Though the characters
just mentioned are all valid, element names
can only start with letters or underscores (or
colons). On top of that, you should not begin
element names with "xml" as it is
also, technically, restricted. Naming is also
case sensitive. So if you start a non-empty
element with <tag> it must end in </tag>
to properly close it and not, for instance,
</Tag>. Typically XML tags are written
in lowercase, and should you place HTML within
XML, the HTML is often uppercase.
Unlike folders in a directory on your computer,
elements can exist within the same parent element
and share the same name. It is quite ok to have
more than one to element in the letter example.
Each element still remains unique in its own
right even though it's name is shared among
one or others within the parent (or document
for that matter). This makes it much easier
to send a letter to more than one person!
- <letter>
- <to>Lisa</to>
- <to>Jenna</to>
- <to>Karen</to>
- <from>Peter</from>
- <body>I
love you!</body>
- </letter>
|