Category Archives: XML

XML Node Parents

Actionscript:
  1. function numParents(e:XML):int {
  2.     var num:int = 0;
  3.     while (e.parent()!= null) {
  4.         num++;
  5.         e = e.parent();
  6.     }
  7.     return num;
  8. }

Sometimes when reading through XML it's helpful to know how many parents a given node has. This function will do that. I recently used this function in an accordion widget.

Posted in XML | Tagged , | Leave a comment

Yahoo Weather RSS

Actionscript:
  1. var temp:Number;
  2. var weatherData:XML;
  3. var yweather:Namespace;
  4.  
  5. var zipcode:String="11211";
  6. var units:String = "f";  // "f" or "c" for Fahrenheit or Celsius
  7.  
  8. var yahooURL:String = "http://weather.yahooapis.com/forecastrss?p=" + zipcode + "&u=" + units;
  9.  
  10. var yahooWeather:URLLoader = new URLLoader();
  11.  
  12. yahooWeather.load(new URLRequest(yahooURL));
  13. yahooWeather.addEventListener(Event.COMPLETE, onLoaded);
  14.  
  15. function onLoaded(evt:Event):void {
  16.  
  17.     weatherData=new XML(evt.target.data);
  18.     yweather = weatherData.namespace("yweather");
  19.    
  20.     temp = weatherData.channel.item.yweather::condition.@temp;
  21.      
  22.     trace(temp);
  23. }

This snippet comes from a student question about how to get the temperature from yahoo weather.

You could use this snippet to get lots of additional information based on a zipcode... like latitude, longitude, humidity, wind chill etc... The trickiest part of this code is the Namespace stuff... which I find generally annoying... probably just because I'm not used to it.

You can read some detailed info about the content contained in the RSS file here.

Also posted in external data | Tagged , , , , , , | Leave a comment

DisplayObject describeType()

Actionscript:
  1. for each(var prop:String in describeType(DisplayObject).factory.accessor.@name){
  2.     trace(prop);
  3. }
  4. /* outputs
  5. scaleY
  6. mouseX
  7. mouseY
  8. mask
  9. rotation
  10. alpha
  11. transform
  12. blendMode
  13. x
  14. root
  15. loaderInfo
  16. width
  17. z
  18. rotationX
  19. scale9Grid
  20. filters
  21. rotationY
  22. y
  23. stage
  24. scaleZ
  25. parent
  26. accessibilityProperties
  27. scrollRect
  28. rotationZ
  29. height
  30. name
  31. opaqueBackground
  32. blendShader
  33. cacheAsBitmap
  34. visible
  35. scaleX
  36. */

I First saw describeType() at senocular's AS3 Tip of the Day on Kirupa.

Also posted in DisplayObject | 1 Comment