Home arrow Flash arrow Page 3 - Using XML and ActionScript with Flex Applications
FLASH

Using XML and ActionScript with Flex Applications


In this conclusion to a five-part series on using ActionScript to build sophisticated programs in Flex, we will add XML to the equation. It is excerpted from chapter four of the book Programming Flex 2, written by Chafic Kazoun and Joey Lott (O'Reilly, 2007; ISBN: 059652689X). Copyright 2007 O'Reilly Media, Inc. All rights reserved. Used with permission from the publisher. Available from booksellers or direct from O'Reilly Media.

Author Info:
By: O'Reilly Media
Rating: 3 stars3 stars3 stars3 stars3 stars / 20
May 29, 2008
TABLE OF CONTENTS:
  1. · Using XML and ActionScript with Flex Applications
  2. · Reading XML Data
  3. · Writing to and Editing XML Objects
  4. · Reflection
  5. · Getting the Class by Name

print this article
SEARCH DEVARTICLES

Using XML and ActionScript with Flex Applications - Writing to and Editing XML Objects
(Page 3 of 5 )

You can also write to and edit XML objects using ActionScript. There are three things you can do in this category:

  1. Modify existing data.
  2. Add new data.
  3. Remove existing data.

Using XML

You can modify existing data using the same E4X syntax you use to read the data on the left side of an assignment statement. For example, the following changes thetitleof the firstbook:

  xml.book[0].title = "Programming Flex 2: Edition 1";

The following example changes the name of the secondauthorof thefirstbook:

  xml.book[0].authors.author[1].@first = "Joseph";

If you want to add new data, you can use theappendChild(),prependChild(),insertChildBefore(), andinsertChildAfter()methods. Each method inserts a new XML node into anXMLorXMLListstructure. TheappendChild()andprependChild()methods each accept one parameter and insert the node at the end and at the beginning of the structure, respectively. The following adds a new publisher node to each book:

  xml.book[0].appendChild(<publisher>O'Reilly</publisher>);
  xml.book[1].appendChild(<publisher>O'Reilly</publisher>);

You can use theinsertChildBefore()andinsertChildAfter()methods to add a new node before or after an existing node. The methods each require two parameters: the new node to add, and a reference to the existing node. The following adds a new publication date node (publicationDate) between the authors and publisher nodes of the books:

  xml.book[0].insertChildAfter(xml.book[0].authors, <publicationDate>2006</
  publicationDate>);
  xml.book[1].insertChildAfter(xml.book[1].authors, <publicationDate>2006</
  publicationDate>);

You can remove elements using thedeleteoperator. The following example first adds a new middle attribute to an author node and then removes it:

  xml.book[0].authors.author[1] = <author first="Joey" middle="Persnippity" last="Lott"
  />;
  trace(xml.book[0].authors);
  delete xml.book[0].authors.author[1].@middle;
  trace(xml.book[0].authors);


blog comments powered by Disqus
FLASH ARTICLES

- More Top Flash Game Tutorials
- Top Flash Game Tutorials
- Best Flash Photo Gallery Tutorials
- The Top Flash Tutorials for Menus
- 7 Great Flash Tutorials
- Adobe Creative Suite 5.5 Now Available
- Critical Flash Vulnerability Heats Up the Web
- More on Nonpersistent Client-Side Remote Sha...
- Nonpersistent Client-Side Remote Shared Obje...
- Using the Decorator Pattern for a Real Web S...
- Using Concrete Decorator Classes
- Delving More Deeply into the Decorator Patte...
- The Decorator Pattern in Action
- A Simple Decorator Pattern Example
- Decorator Pattern

Watch our Tech Videos 
Dev Articles Forums 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
Contact Us 
Site Map 
Privacy Policy 
Support 

Developer Shed Affiliates

 




© 2003-2017 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap
Popular Web Development Topics
All Web Development Tutorials