Converting XML and XMLList to a String

As we've seen throughout this chapter, E4X implements custom rules for converting XML and XMLList instances to a string. For reference and review, this section describes E4X's XML-to-string and XMLList-to-string conversion rules. Remember that an XML instance can represent five different kinds of content: an element, an attribute, a text node, a comment, or a processing instruction. We'll consider the conversion rules for each kind separately, but we'll start with XMLList-to-string conversion in preparation for the subsequent discussion.

Converting XMLList to a String

When an XMLList has only one XML instance, the result of XMLList's toString( ) is exactly the same as the result of calling toString( ) on that one instance. For example, in the following code, the title variable refers to an XMLList whose single XML instance represents the <TITLE> element. Converting title to a string yields Ulysses, exactly as if toString( ) had been invoked on the single XML instance directly:

var novel:XML = <BOOK ISBN="0141182806">
    <AUTHOR>Joyce, James</AUTHOR>
    <PUBLISHER>Penguin Books Ltd</PUBLISHER>
// Create an XMLList with only one XML instance
var title:XMLList = novel.TITLE;
// Convert the XMLList to a string, and display that string.
trace(title);  // Displays: Ulysses

When an XMLList has more than one XML instance, XMLList's toString( ) returns the result of calling toXMLstring( ) on each XML instance and concatenating those ...

Get Essential ActionScript 3.0 now with O’Reilly online learning.

O’Reilly members experience live online training, plus books, videos, and digital content from 200+ publishers.