Introduction to XML in Flash
by senocular
White Space in Loaded XML
There is a certain XML option concerning loaded
XML that should not go without mention. That
is the option to ignore extraneous white space
between elements in an XML document. This is
determined by an ignoreWhite property
of an XML instance.
- my_xml.ignoreWhite
= true;
What this does is prevents white space such
as tabs and spaces used in formatting in your
XML to be interpreted as text nodes which Flash
likes to do (white space is text too right?).
For example. How many child nodes does the happy
element have here:
- <happy>
- <joy />
- </happy>
If you said three, then you were right! The
happy element has one child element, joy, and
two text nodes; a newline + tab text
node before joy and another newline
text node following it. This effect is generally
not desired as such white space is meant solely
for formatting purposes. By default, the ignoreWhite
property for any XML instance in Flash is set
to false, so you may want to get into the habit
of setting it to true immediately after creating
an XML instance if you don't want such white
space to be considered text elements. Here it
is applied to the previous example used to load
my_documents.xml:
- var my_xml
= new
XML();
- my_xml.ignoreWhite
= true;
- my_xml.onLoad
= function(success){
- if (success){
- trace(this);
- }
- }
- my_xml.load("my_document.xml");
The ignoreWhite property should be set before
XML is loaded as it effects the parsing process.
It won't change existing XML within an XML instance.
Note: ignoreWhite only removes white space
between elements, this doesn't not
include any white space that makes up valid
text nodes, even that which is used for formatting
them.
Tracing XML
If you've tried the above script, you'll notice
that the XML instance traces out the entire
XML document it contains when passed to a trace
command. Many objects in Flash put "[object
Object]" in the output window when traced.
The XML object, however, has a unique toString
method (the method used by objects represent
themselves when trace needs to show it in the
output window) that overrides the "[object
Object]" with the string representation
of the actual XML contents. When you set ignoreWhite
to true, you can see in a trace how the XML's
structure has changed as a result of that property.
For example, the happy-joy XML with ignoreWhite
set to true would trace "<happy><joy
/></happy>." Later, we'll show
you how you can make a toString-esque method
that can effectively reverse this process.
|