Recipe 20.8.
Reading Text Nodes and Their Values
Problem
You want to extract the value from a text
node.
Solution
Use E4X
syntax, or, alternatively, use the text( ) method to return
an XMLList of text
nodes for an element. Then use the toString( ) method to convert the
text node's value to a string, use a conversion function such as
int( ) or Number( ) to convert the value to another
datatype, or let the Flash Player automatically convert the value
to a specific type.
Discussion
Recipe
20.4 looks at text nodes and how to create them in XML objects.
This recipe explains how to extract the value from a text node. You
can get a reference to the text node in much the same as you
reference element nodes, discussed in Recipes 20.6
and 20.7.
Once you have a reference to the text node, you can extract its
value by using toString( ) or a conversion method on the
node. For example, consider the following XML packet:
<book>
<title>ActionScript 3.0 Cookbook</title>
</book>
The root node of this XML packet is
<book>, which contains <title> as its
child element. The <title> element, in turn,
contains a nested node; a text node with a value of
ActionScript 3.0 Cookbook.
If this XML packet is assigned to an XML
object, you can "dot down"
with E4X syntax to get to the title element node by name,
and then use the toString( ) method to access the text node
child of title:
var book:XML = <book>
<title>ActionScript 3.0 Cookbook</title>
</book>;
// Use E4X syntax to access the title element, and assign
// it to the title variable.
var title:String = book.title.toString( );
// Displays: ActionScript 3.0 Cookbook
trace( title );
In this example, converting the title
element node to a String is done by explicitly invoking the
toString( ) method. In truth, you can omit the call to
toString( ) because the Flash Player does this for us
automatically. However, it is generally a good idea to explicitly
use toString( ) so your code communicates its intent.
Similarly, you can convert a text node to other
types besides String. The following example demonstrates
converting text nodes to various types by using both conversion
functions Boolean( ) and int( ), and letting the
Flash Player automatically do the conversion for the Number
type (instead of explicitly using Number( )):
var example:XML = <example>
<bool>true</bool>
<integer>12</integer>
<number>.9</number>
</example>;
// Convert a text node of "true" to boolean true
var bool:Boolean = Boolean( example.bool );
// Convert a text node of "12" to an integer
var integer:int = int( example.integer );
// Convert a text node of ".9" to a number
var number:Number = example.number;
/* Displays:
true
12
.9
*/
trace( bool );
trace( integer );
trace( number );
The preceding example is slightly misleading.
The Boolean( ) conversion function may not work exactly the
way you think it does. Changing the text node value in the
<bool> element from TRue to false
results in the bool variable being set to TRue,
and not false like you might think. This behavior is by
design, frustrating as it may be. A simple technique to convert the
string false to a Boolean false is to use the
toLowerCase( ) method and compare the value with the string
true:
var bool:Boolean = example.bool.toLowerCase( ) == "true";
Until now we've examined element nodes with a
single text node as a child. What about mixed elements? Trying to
convert a mixed element into a string results in a formatted XML
string as output:
var fruit:XML = <fruit>
<name>Apple</name>
An apple a day...
</fruit>;
// Explicity using toString( ) here is required
var value:String = fruit.toString( );
/* Displays:
<fruit>
<name>Apple</name>
An apple a day...
</fruit>
*/
trace( value );
In these situations, the text( ) method
should be used to return only the text nodes of an element. As
demonstrated in Recipe
20.6, use a for each loop to iterate over the nodes:
var fruit:XML = <fruit>
<name>Apple</name>
An apple a day...
</fruit>;
for each ( var textNode:XML in fruit.text( ) ) {
// Displays: An apple a day...
trace( textNode );
}
See Also
Recipes 20.4,
20.6,
and 20.7
|