Archive for the ‘xml’ Category

HOW TO: Delete an XML node using E4X

Monday, April 21st, 2008 |

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: 113
description: Code source (.FLA) for delete XML node using E4X in ActionScript 3 sample

E4X XML Namespaces

Tuesday, November 6th, 2007 |

For XML that looks like the following response from Yahoo's weather service:

XML:
  1. &lt;?xml version='1.0' encoding='UTF-8'?&gt;
  2. http://us.rd.yahoo.com/dailynews/rss/weather/Sunnyvale__CA/*
  3. http://weather.yahoo.com/forecast/94089_f.html         Yahoo! Weather for Sunnyvale, CA
  4. en-us
  5. Tue, 06 Nov 2007 6:56 pm PST
  6. 60
  7.  
  8.  
  9.  
  10.  
  11.  
  12. <img alt="" />
  13.  
  14. 142
  15. 18
  16.  
  17. http://weather.yahoo.com/             http://l.yimg.com/us.yimg.com/i/us/nws/th/main_142b.gif
  18. 37.39
  19. -122.03
  20.  
  21. http://us.rd.yahoo.com/dailynews/rss/weather/Sunnyvale__CA/*http://weather.yahoo.com/forecast/94089_f.html Tue, 06 Nov 2007 6:56 pm PST
  22.  
  23. &lt;![CDATA[
  24. <img src="http://l.yimg.com/us.yimg.com/i/us/we/52/33.gif" alt="" />
  25. <strong>Current Conditions:</strong>
  26. Fair, 55 F
  27.  
  28. <strong>Forecast:</strong>
  29. Tue - Mostly Clear. High: 67 Low: 49
  30. Wed - Partly Cloudy. High: 71 Low: 49
  31.  
  32. <a href="http://us.rd.yahoo.com/dailynews/rss/weather/Sunnyvale__CA/* http://weather.yahoo.com/forecast/94089_f.html">Full Forecast at Yahoo! Weather</a>
  33. (provided by The Weather Channel)
  34. ]]&gt;
  35.  
  36.  
  37.  
  38. 94089_2007_11_06_18_56_PST

When you need to access the nodes with complex node names such as yweather:location think XML namespaces.
In the above example, look for the xmlns declaration in the rss node
- xmlns:yweather='http://xml.weather.yahoo.com/ns/rss/1.0'.
Creating a Namespace object using the declaration will allow us to access the nodes with complex names.
Creating a Namespace object is a pretty simple process:

Actionscript:
  1. var yweatherNS:Namespace = new Namespace( http://xml.weather.yahoo.com/ns/rss/1.0 );

Now we can access the node with the following code:

Actionscript:
  1. yahooWeaterXML.channel.yweatherNS::location
  2.  
  3. trace( yahooWeaterXML.channel.yweatherNS::location.@city ); // outputs 'Sunnyvale'
  4. trace( yahooWeaterXML.channel.yweatherNS::location.@region); // outputs 'CA'
  5. trace( yahooWeaterXML.channel.yweatherNS::location.@country ); // outputs 'US'

With the preceding example we need to know the namespace url ahead of time. E4X gives us the namespaceDeclarations() method that will return an array that contains the namespace declarations associated with the XML document

Using the namespaceDeclarations() method from the XML object:

Actionscript:
  1. var namespaces:Array = myXML.namespaceDeclarations();

Now we have an array of namespace declarations that we can use to dynamically declare Namespace objects and access our complex node names:

Actionscript:
  1. var yweatherNS:Namespace
  2. var geoNS:Namespace
  3. var nsLen:uint = nameSpaces.length;
  4. for(var i:uint = 0; i &lt;nsLen; i++)
  5. {
  6. var newNamespace:Namespace = new Namespace( nameSpaces[i] );
  7. if( String( nameSpaces[i].prefix ).toLowerCase() == "yweather" )
  8. {
  9. yweatherNS = newNamespace;
  10. }
  11. else
  12. {
  13. geoNS = newNamespace;
  14. }
  15. }

So we can access the geo and yweather nodes without any problems now.

Find entries :

Want to subscribe?

  Subscribe in a reader or,
Subscribe via email:

About me

I'm a senior developer at RealEyes Media, Adobe Certified Instructor and Adobe Certified Professional. Here you'll get my ideas and experience Flex, Flash, ColdFusion and related technologies as well as some generally off the wall stuff.