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

Changing or Creating New XML Content

In E4X, most common additions and modifications to an existing XML instance can be achieved using simple assignment statements. E4X assignments, however, have different results depending on the type of value being assigned and the target of the assignment. Let's look at the various scenarios one at a time.

Changing the Contents of an Element

To change the contents of an XML element, we assign that element any value other than an XMLList or XML object. The value is converted to a string and replaces the element's content. Recall our <BOOK> fragment:

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

To change the contents of the <TITLE> element from "Ulysses" to "The Sun Also Rises", we use:

novel.TITLE[0] = "The Sun Also Rises";

But remember that E4X lets us treat an XMLList like an XML object wherever possible, so because the XMLList returned by novel.TITLE has a single XML instance only, we can use this more convenient code:

novel.TITLE = "The Sun Also Rises";  // Removed [0]

However, if the XMLList returned by novel.TITLE had more than one <TITLE> element, the assignment would have a different meaning, as described in the later section "Assigning Values to an XMLList." (If you need a refresher on the difference between novel.TITLE[0] and novel.TITLE, see the earlier section, "Treating XMLList as XML, Revisited.")

Now let's change the author and publisher of the ...

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