Skip to content


HOW TO: Delete an XML node using E4X

While creating a sample application for some class content the other day, I ran into an XML/E4X situation that I'd never encountered before. It is pretty basic - deleting a node from XML. How does one do it? With the delete keyword of course!

For example:

Actionscript:
  1. var  myXML:XML =
  2.     <order>
  3.         <item id="0" name="main">Hamburger</item>
  4.         <item id="1" name="side">Fries</item>
  5.         <item id="2" name="drink">Med. Soda</item>
  6.         <item id="3" name="drink">Lg. Soda</item>
  7.     </order>;
  8.  
  9. // Delete the Med. Soda node
  10.  
  11. // Output the XML
  12. trace( myXML.toXMLString() );
  13.  
  14. // Result
  15. //<order>
  16. //  <item id="0" name="main">Hamburger</item>
  17. //  <item id="1" name="side">Fries</item>
  18. //  <item id="2" name="drink">Med. Soda</item>
  19. //  <item id="3" name="drink">Lg. Soda</item>
  20. //</order>
  21.  
  22. delete myXML.item[2];
  23.  
  24. // Output the edited XML
  25. trace( myXML.toXMLString() );
  26.  
  27. // Result
  28. //<order>
  29. //  <item id="0" name="main">Hamburger</item>
  30. //  <item id="1" name="side">Fries</item>
  31. //  <item id="3" name="drink">Lg. Soda</item>
  32. //</order>

Man, E4X is so simple!

http://john.realeyes.com/wp-content/plugins/downloads-manager/img/icons/winzip.gif download: Delete Node Example (4.78KB)
added: 04/09/2009
clicks: 288
description: Code source (.FLA) for delete XML node using E4X in ActionScript 3 sample

Posted in news. Tagged with , , .