Introduction to XML in Flash
by senocular
Text Nodes
Text nodes are what make up the bulk of your
text, especially body text, in XML. Unlike elements,
they themselves do not need an opening or closing
tag, mainly because, well, they simply have
no tags at all. They're just text. The previous
example had three text nodes, Sandy, Peter,
and I love you!
- <letter>
- <to>Sandy</to>
- <from>Peter</from>
- <body>I
love you!</body>
- </letter>
Though all single lined above, text nodes can
span multiple lines and contain most types of
text. However, because of the very form of XML
and how it's structured using tags (with elements),
there are certain characters in a text node
that cannot be used or it will break the document
formatting. If this happens, the document is
said not to be well formed.
These certain characters will have to be replaced
by character entity references. Strictly
speaking only "<" and "&"
are restricted (in their typical use and that
can depend on the environment you're working
in), though there are 5 predefined entity references
for XML that you should be aware of. They are
as follows:
Character |
Replace with |
< |
< |
> |
> |
& |
& |
' |
' |
" |
" |
Example:
- <incorrect>
- It is clearly stated that variable A <
variable B & variable A > variable
C
- </incorrect>
- <acceptable>
- It is clearly stated that variable A <
variable B & variable
A > variable C
- </acceptable>
Notice that it was not completely
necessary to replace the ">" character,
only < and &, though it may be in your
best interest to do so for good practice and
consistency and, you know, just because it's
the right thing to do.
- <best>
- It is clearly stated that variable A <
variable B & variable
A > variable C
- </best>
One thing that is easily forgotten is that
text nodes are nodes - they are distinctly separate
from element nodes and not a part of them. In
fact, they are children of element nodes. Sandy
is a child node of the to element and the to
element is Sandy's parent. When getting into
extracting content from XML, this is easily
overlooked as, often, some elements' sole purpose
is to contain a text node.
|