O'Reilly logo

Essential ActionScript 3.0 by Colin Moock

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Creating XML Data with E4X

To create the <BOOK> XML fragment from Example 18-1 via E4X, we have three general options:

  • Use the XML constructor to create a new XML instance, and then create the remainder of the fragment programmatically using the techniques covered in later section "Changing or Creating New XML Content."

  • Use the XML constructor to create a new XML instance, and then import the fragment from an externally loaded file, as discussed in the later section "Loading XML Data."

  • Write our XML data in literal form, just like a string or a number, anywhere literals are allowed by ActionScript.

For now, we'll use the third approach—creating the XML fragment with an XML literal. Example 18-2 demonstrates; it assigns a literal XML value (the XML fragment from Example 18-1) to the variable novel.

Example 18-2. Assigning an XML literal to a variable

var novel:XML = <BOOK ISBN="0141182806">
    <TITLE>Ulysses</TITLE>
    <AUTHOR>Joyce, James</AUTHOR>
    <PUBLISHER>Penguin Books Ltd</PUBLISHER>
  </BOOK>;

When the preceding code runs, ActionScript generates a new E4X XML instance representing the literal XML fragment and assigns it to the variable novel.

Tip

To view the XML source code for an XML instance (such as the one referenced by novel), use the XML class's instance method toXMLString( ), as in:

trace(novel.toXMLString());

The toXMLString( ) method is covered later in the section "Converting XML and XMLList to a String."

Notice that the line breaks and quotation marks in the preceding XML literal are ...

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, interactive tutorials, and more.

Start Free Trial

No credit card required