Category Archives: XML

I’m Twitter Search

Actionscript:
  1. [SWF(width = 400, height=600, frameRate=12)]
  2. var loader:URLLoader = new URLLoader();
  3. var req:URLRequest = new URLRequest("http://search.twitter.com/search.atom");
  4. var vars:URLVariables = new URLVariables();
  5. vars.q = "I'm";
  6. // results per page
  7. vars.rpp = "7";
  8. vars.page = 1;
  9.  
  10. req.data = vars;
  11. req.method = URLRequestMethod.GET;
  12.  
  13. loader.addEventListener(Event.COMPLETE, onLoaded);
  14. loader.load(req);
  15.  
  16. // check for updates every 30 seconds -
  17. var timer:Timer = new Timer(30000);
  18. timer.addEventListener(TimerEvent.TIMER, onTick);
  19. timer.start();
  20. function onTick(evt:TimerEvent):void{
  21.     txt.htmlText = "loading...";
  22.     loader.load(req);
  23. }
  24.  
  25. var txt:TextField = TextField(addChild(new TextField()));
  26. txt.defaultTextFormat = new TextFormat("_sans", 12);
  27. with (txt){ x=10, y=10, width=stage.stageWidth-10;
  28. height=stage.stageHeight, multiline = true, wordWrap = true; }
  29. txt.htmlText = "loading...";
  30.                                                    
  31. function onLoaded(evt:Event):void{
  32.     var searchData:XML = new XML(loader.data);
  33.     var atom:Namespace = searchData.namespace("");
  34.     default xml namespace = atom;
  35.     var htmlText:String = "<b>Last " + vars.rpp + " '" + vars.q + "' tweets:</b><br><br>";
  36.     for each(var entry:XML in searchData.entry){
  37.         htmlText += entry.author.name.toString() + ": <br>";
  38.         htmlText += entry.title.toString() + "<br><br>";
  39.     }
  40.     txt.htmlText = htmlText;
  41. }

Was curious about the twitter search api so spent a few minutes and whipped up a quick demo - the api is simple and easy to understand...

As a test I created something that shows the last 7 tweets including the phrase I'm. A new search is done every 30 seconds... I chose the phrase I'm so that there will be different results every time a new search is done...

Read More »

Also posted in external data | Tagged , , , | 1 Comment

XML to ActionScript #3 (AsXML)

XML:
  1. <code>
  2.   <make reference="w" class="BasicView" args="stage.stageWidth, stage.stageHeight, false"/>
  3.   <call method="addChild" args="w"/>
  4.  
  5.   <make reference="wireMat" class="WireframeMaterial" args="0x000000" />
  6.  
  7.   <make reference="sphere" class="Sphere" args="wireMat, 100" />
  8.  
  9.   <call method="w.scene.addChild" args="sphere" />
  10.  
  11.   <make reference="animation" class="Object">
  12.     <set z="-500" rotationY="360"  rotationX="360" ease="Back.easeOut"/>
  13.   </make>
  14.  
  15.   <call method="TweenLite.to" args="sphere, 3, animation" />
  16.  
  17.   <call method="setInterval" args="w.singleRender, 32" />
  18.  
  19. </code>

This snippet shows XML that the mini-library AsXML can read and run - in this case AsXML is set up to run with Papervision

A few days ago I had the idea to write some code that would run ActionScript based on XML. I spent some time getting rid of a few bugs and setting up some demos with TweenLite, Papervision and QuickBox2D. I wrapped everything up into a mini-library called AsXML.

Check out the demos here.


Download AsXML and demo files here.

AsXML Features:
1) call methods of the main timeline
2) read and write properties on the main timeline
3) instantiate classes on the main timeline
4) call methods on these classes
5) read and write properties on these classes
6) store references to return values from functions

Also posted in Box2D, Graphics, Math, QuickBox2D, dynamic, external data, instantiation, misc, motion, return values, string manipulation, strings | Tagged , , | 11 Comments

XML to ActionScript #2

Actionscript:
  1. var script:XML=<code>
  2.  
  3.    <make reference="blur" class="flash.filters.BlurFilter">
  4.      <set blurX="10" blurY="10" />
  5.    </make>
  6.    
  7.    <make reference="filts" class="Array">
  8.      <call method="push">
  9.        <arg reference="blur" />
  10.      </call>
  11.    </make>
  12.    
  13.    <make reference="mat" class="flash.geom.Matrix">
  14.      <call method="rotate" args="1" />
  15.      <set tx="100" ty="100" />
  16.    </make>
  17.    
  18.    <make reference="box" class="flash.display.Sprite">
  19.        <setRef filters="filts" transform.matrix="mat"/>
  20.        <call method="graphics.beginFill" args="0x000000" />
  21.        <call method="graphics.drawRect" args="-50,-50,100,100" />
  22.    </make>
  23.    
  24.    <call method="addChild">
  25.      <arg reference="box"/>
  26.    </call>
  27.    
  28.    <make reference="tf" class="flash.text.TextFormat">
  29.     <set font="_sans" size="12" color="0xFFFFFF" />
  30.    </make>
  31.  
  32.    <make reference="txt" class="flash.text.TextField">
  33.      <setRef defaultTextFormat="tf" />
  34.      <set autoSize="center" text="XML to AS3" />
  35.    </make>
  36.  
  37.    <make reference="circle" class="flash.display.Sprite">
  38.        <set x="300" y="300" />
  39.        <call method="graphics.beginFill" args="0xFF0000" />
  40.        <call method="graphics.drawCircle" args="0,0,100" />
  41.        <call method="addChild">
  42.          <arg reference="txt" />
  43.        </call>
  44.    </make>
  45.    
  46.    <call method="addChild">
  47.      <arg reference="circle"/>
  48.    </call>
  49.  
  50. </code>
  51.  
  52. // parse and run
  53. runCode(this, script);
  54.  
  55. function runCode(target:*, code:XML):void{
  56.     var children:XMLList = code.children();
  57.     for (var i:int = 0; i<children.length(); i++){
  58.         var child:XML = children[i];
  59.         var type:String = child.name();
  60.         if (type == "call"){
  61.             runMethod(target, child);
  62.         }else if (type == "set"){
  63.             setProp(target, child);
  64.         }else if (type == "setRef"){
  65.             setRefProp(target, child);
  66.         }else if (type == "make"){
  67.             makeInstance(child);
  68.         }
  69.     }
  70. }
  71.  
  72. function makeInstance(code:XML):void{
  73.     var ClassRef:Class = getDefinitionByName(code.@["class"]) as Class;
  74.     // nothing can be passed to the class constructor
  75.     // its not possible with function.apply
  76.     var instance:* = this[code.@reference] = new ClassRef();
  77.     runCode(instance, code);
  78. }
  79.  
  80. // set a property to a reference
  81. function setRefProp(target:*, code:XML):void{
  82.     var attributes:XMLList = code.attributes();
  83.     for (var i:int = 0; i<attributes.length(); i++){
  84.         var prop:String = attributes[i].name();
  85.         var o:Object = dotSyntax(target, prop, 1)
  86.         if (prop.indexOf(".") != -1){
  87.           o.obj[o.prop] = dotSyntax(this, attributes[i]);
  88.         }else{
  89.           o.obj[o.prop] = this[attributes[i]];
  90.         }
  91.     }
  92. }
  93.  
  94. // set a property to a value such as a Number, Boolean, String etc...
  95. function setProp(target:*, code:XML):void{
  96.     var attributes:XMLList = code.attributes();
  97.     for (var i:int = 0; i<attributes.length(); i++){
  98.         var prop:String = attributes[i].name();
  99.         var o:Object = dotSyntax(target, prop, 1)
  100.         o.obj[o.prop] = valueType(attributes[i]);
  101.     }
  102. }
  103.  
  104. function runMethod(target:*, code:XML):void{
  105.     var i:int = 0;
  106.     // get a reference to the function
  107.     var method:Function = dotSyntax(target, code.@method);
  108.     // call the function if there are no arguments
  109.     var attributeArgsLength:int = code.@args.toXMLString().length;
  110.     var childArgsLength:int = code.arg.length()
  111.     if (attributeArgsLength == 0 && childArgsLength == 0){
  112.         method();
  113.     }else{
  114.      
  115.      var args:Array;
  116.      if (attributeArgsLength> 0){
  117.          args = code.@args.toString().split(",");
  118.      } else {
  119.         args = [];
  120.      }
  121.    
  122.      for (i = 0; i<childArgsLength; i++){
  123.           var val:String = code.arg[i].@value;
  124.           if (val){
  125.             args.push(val);
  126.           }
  127.       }
  128.       for (i = 0; i<args.length; i++){
  129.           if (args[i] != ""){
  130.             args[i] = valueType(args[i]);
  131.           }
  132.       }
  133.       for (i = 0; i<code.arg.length(); i++){
  134.          var rs:String = code.arg[i].@reference;
  135.          if (rs){
  136.              args.push(dotSyntax(this, rs));  
  137.          }
  138.       }
  139.      
  140.       // run the function
  141.       method.apply(null,args);
  142.     }
  143. }
  144.  
  145. // parse dot syntax and return the last property or method
  146. function dotSyntax(target:*, str:String, offset:int = 0):*{
  147.     var path:Array = str.split(".");
  148.     var curr:* = target;
  149.     for (var i:int = 0; i<path.length - offset; i++){
  150.         curr = curr[path[i]]
  151.     }
  152.     if (offset != 0){
  153.         return {obj:curr, prop:path[i]}
  154.     }
  155.     return curr;
  156. }
  157.  
  158. function valueType(val:*):* {
  159.     if (isNaN(Number(val))) {
  160.            // remove leading and trailing white
  161.            // remove "" around strings
  162.            val = val.replace(/^s+|s+$/g,"").replace(/^"|"$/g,"");
  163.            //  see if it's a boolean
  164.            if (val == "true"){
  165.                val = true;
  166.            }else if (val == "false"){
  167.                val = false
  168.            }
  169.     } else {
  170.         val = Number(val);
  171.     }
  172.     return val;
  173. }

WARNING: There area few small bugs in this snippet. If you'd like to use this, check out the AsXML mini-library

This is the next version of yesterdays snippet. As you can see it isn't really a snippet anymore... This code parses a specifically formatted xml file to allow the following features to be achieved from XML at runtime:

1) call methods of the main timeline
2) read and write properties on the main timeline
3) instantiate classes on the main timeline
4) call methods on these classes
5) read and write properties on these classes

By implement those five feature a great deal becomes possible. The above XML creates this somewhat crappy looking thing:

While that doesn't look like much, it's actually doing quite a bit... It has a BlurFilter, an Array (for the filters property), two Sprites, a TextFormat and a TextField. addChild() is called on the main timeline and on one of the sprites (to nest the TextField in the circle).

Uses For This

You could use this to create levels for a simple game.

You could generate this XML based on user input to create e-cards and mini-apps.

If you have a medium sized app you could use it to create a sort of advanced config file that helps to ease your pain as the client decides they need 10 subtly different versions of the app. Every time the client decides they need a subtly different version you'd just need to create a different config file. THIS is probably the thing that I'll be using it for - could be a huge time saver...

Uses With Libraries

This could be used with TweenLite and/or Papervision. You could just add a bunch of import statements to the timeline or to a dynamic document class. Then you'd be able to do some basic authoring from the XML. I think I'll post an example of this either tonight or tomorrow.

I did originally start writing this thinking about QuickBox2D - but I already have an editor for QuickBox2D that just needs some UI (currently its key controlled). So I don't really need it. The QuickBox2D editor generates an ActionScript file - which makes sense because usually you need to go in and manually do a bunch of logic that couldn't really be done easily with an editor. That said, this will work with Box2D or QuickBox2D as an XML format.

The Downside

The downside is that this is dynamic and is therefore untyped. All class instances created by reading the XML will be dynamically typed. For something where your only adding 10 classes this way, its no big deal - but for a game editor it could be a problem - at the very least there are interesting advanced techniques implemented in this snippet that could be repurposed for lots of different things.

More Examples

Tonight or tomorrow I'll upload a few examples - it would be fun to do a TweenLite/Papervision example... would also be good to show what I mean by an advanced config for a medium sized app.

Also posted in dynamic, external data, instantiation | Tagged , , | 2 Comments

XML to ActionScript

Actionscript:
  1. // this xml is inline but could easily be in an external file
  2. var script:XML=<code>
  3.  
  4.   <!-- // call some methods of the main timeline graphics property -->
  5.   <call method="graphics.beginFill" args="0xFF0000" />
  6.    
  7.   <call method="graphics.drawRect" args="0, 0">
  8.      <!-- // use this to access properties of the main movie -->
  9.      <!-- // and use them as arguments -->
  10.      <arg reference="stage.stageWidth" />
  11.      <arg reference="stage.stageHeight" />
  12.   </call>
  13.  
  14.   <call method="graphics.endFill" args=""/>
  15.  
  16.   <call method="graphics.beginFill" args="0x0000FF">
  17.      <!-- //regular non-reference arguments can be passed like this -->
  18.      <arg value="0.5" />
  19.   </call>
  20.  
  21.   <call method="graphics.drawCircle" args="100,100,20" />
  22.  
  23.   <call method="customFunction" args="hello">
  24.      <arg reference="root.loaderInfo.bytesTotal" />
  25.   </call>
  26.  
  27. </code>
  28.  
  29. function customFunction(a:String, b:String):void{
  30.     trace("I am a custom function called from xml");
  31.     trace("Here is an argument: " + a);
  32.     trace("Here is another argument, (total bytes of this swf): " + b);
  33. }
  34.  
  35. // parse and run
  36. runCode(script);
  37.  
  38. function runCode(code:XML):void{
  39.     var children:XMLList = code.children();
  40.     for (var i:int = 0; i<children.length(); i++){
  41.         var child:XML = children[i];
  42.         var type:String = child.name();
  43.         if (type == "call"){
  44.             runMethod(child);
  45.         }
  46.     }
  47. }
  48.  
  49. function runMethod(code:XML):void{
  50.     var i:int = 0;
  51.     // get a reference to the function
  52.     var method:Function = dotSyntax(code.@method);
  53.     // call the function if there are no arguments
  54.     if (code.@args.toXMLString().length == 0 && code.arg.length() == 0){
  55.         method();
  56.     }else{
  57.      
  58.      var args:Array = code.@args.split(",");
  59.      var childArgsLength:int = code.arg.length()
  60.      for (i = 0; i<childArgsLength; i++){
  61.           // add another one with dot syntax
  62.           var val:String = code.arg[i].@value;
  63.           if (val){
  64.             args.push(val);
  65.           }
  66.       }
  67.       for (i = 0; i<args.length; i++){
  68.           if (args[i] != ""){
  69.             args[i] = valueType(args[i]);
  70.           }
  71.       }
  72.       for (i = 0; i<code.arg.length(); i++){
  73.          var rs:String = code.arg[i].@reference;
  74.          if (rs){
  75.              var ref:* = dotSyntax(rs);
  76.              args.push(ref);  
  77.          }
  78.       }
  79.       // run the function
  80.       method.apply(null,args);
  81.     }
  82. }
  83.  
  84. // parse dot syntax and return the last property or method
  85. function dotSyntax(str:String):*{
  86.     var path:Array = str.split(".");
  87.     var curr:* = this;
  88.     for (var i:int = 0; i<path.length; i++){
  89.         curr = curr[path[i]]
  90.     }
  91.     return curr;
  92. }
  93.  
  94. function valueType(val:*):* {
  95.     if (isNaN(Number(val))) {
  96.            // remove leading and trailing white
  97.            // remove "" around strings
  98.            val = val.replace(/^s+|s+$/g,"").replace(/^"|"$/g,"");
  99.            //  see if it's a boolean
  100.            if (val == "true"){
  101.                val = true;
  102.            }else if (val == "false"){
  103.                val = false
  104.            }
  105.     } else {
  106.         val = Number(val);
  107.     }
  108.     return val;
  109. }

WARNING: There area few small bugs in this snippet. If you'd like to use this, check out the AsXML mini-library

A few days ago I had the idea to write some code that would parse ActionScript from XML. The features that I realized would be possible are:

1) call methods of the main timeline
2) read and write properties on the main timeline
3) instantiate classes on the main timeline
4) call methods on these classes
5) read and write properties on these classes

So this morning I wrote the first part. This snippet has the ability to read an XML file and call methods accordingly. It also has the ability to read properties of the main timeline and pass them to these methods.

The XML in this snippet draws a box using the stageWidth and stageHeight. It draws a transparent circle and then calls a custom function. The custom function gets passed the root.loaderInfo.bytesTotal property.

I've done something similar a few times where I specifically targeted the Graphics API. This takes that to the next level by potentially working with any ActionScript classes (excluding Vector maybe... need to think about that).

I'll post another version with more features tomorrow. At the very least it will have class instantiation.

Also posted in dynamic, external data, string manipulation | Tagged , , | 6 Comments

Loop Through All Properties of a Class

Actionscript:
  1. package {
  2.    
  3.     import flash.display.Sprite;
  4.     import flash.utils.describeType;
  5.    
  6.     public class Main extends Sprite {
  7.        
  8.         public function Main(){
  9.             var test:Test = new Test();
  10.             var desc:XML= describeType(test);
  11.             // public vars
  12.             for each (var v:XML in desc.variable){
  13.                 trace(v.@name, test[v.@name]);
  14.             }
  15.             // getters
  16.             for each (v in desc.accessor){
  17.                 trace(v.@name, test[v.@name]);
  18.             }
  19.         }
  20.        
  21.     }
  22. }
  23.  
  24. class Test{
  25.     public var a:Number = 123;
  26.     public var b:Number = 100;   
  27.     private var _getterVal:Boolean = false;
  28.     public function get getter():Boolean{
  29.         return _getterVal;
  30.     }
  31. }
  32. /*
  33. outputs:
  34. b 100
  35. a 123
  36. getter false
  37. */

I'm working on a few libraries, QuickBox2D and a library for auto-generated UI stuff... this technique just came in handy. It shows how to use describeType() to loop through public vars and getters of a given class.

The title of this post should really be Loop Through All PUBLIC properties of a class.... but it was long enough as it is....
Note: this should be run as document class

Also posted in OOP, Object, dynamic | Tagged , | 3 Comments

Graphics Class Methods in XML

Actionscript:
  1. // for simplicity I left this XML inline, this will work exactly the same if it were external
  2. var program:XML=<body>
  3.                 <draw>
  4.                   <![CDATA[
  5.                    beginFill(0xFF0000);
  6.                    drawCircle(100,100,50);
  7.                    endFill();
  8.                    lineStyle(0, 0x666666);
  9.                    moveTo(100, 100);
  10.                    lineTo(200, 200);
  11.                    moveTo(300, 200);
  12.                    curveTo(350, 300, 400, 200);
  13.                    lineStyle(0, 0x0000FF);
  14.                    drawRect(200, 50,100,100) ;
  15.                     ]]>
  16.                 </draw>
  17. </body>;
  18.  
  19. // parse and run the Graphics class commands from the XML
  20. render(parseFunctions(program.draw.toString()));
  21.  
  22. function parseFunctions(dat:String):Array{
  23.     var a:Array = dat.split(";") ;
  24.     for (var i:int = 0; i<a.length-1; i++){
  25.         a[i] = a[i].split(/\(\)|\(|\)/g);
  26.         var f:String = a[i][0] = a[i][0].replace(/\s/g,"");
  27.         a[i] = a[i].splice(0, a[i].length - 1);
  28.         if (a[i].length> 1){
  29.          a[i] = a[i][1].split(",");
  30.          a[i].unshift(f);
  31.         }
  32.     }
  33.     return a.splice(0,a.length - 1);
  34. }
  35. function render(p:Array):void {
  36.     for (var i:int = 0; i<p.length; i++) {
  37.         graphics[p[i][0]].apply(graphics,p[i].splice(1));
  38.     }
  39. }

The above code builds on yesterdays post by showing how one could potentially store graphics class method calls in XML using a few regular expressions and Function.apply().

The parseFunctions() function reads through the CDATA string and formats it in a 2D array that looks like this:

Actionscript:
  1. [[beginFill, 0xFF0000], [drawCircle, 100, 100, 50], etc...]

The render() function reads through this 2D array, using the first value of each nested array as the function and the remaining values as arguments...

As is this won't really work with most of the new fp10 graphics methods...

Also posted in Graphics, dynamic, external data, functions, string manipulation, strings | Tagged , | Leave a comment

E4X and ActionScript Variables

Actionscript:
  1. var backgroundColor:uint = 0xEFEFEF;
  2. var borderColor:uint = 0xFF0000;
  3. var buttonOverColor:uint = 0x0000FF;
  4. var buttonOutColor:uint = 0x00CCCC;
  5.  
  6. var uiColors:XML = <ui>
  7.     <default color="0xCCCCCC" />
  8.     <background color = { backgroundColor } />
  9.    
  10.     <!-- note hexidecimal formatting code -->
  11.     <border color={ ("0x" + borderColor.toString(16)) } />
  12.    
  13.     <button overColor={ buttonOverColor} outColor={ buttonOutColor } />
  14. </ui>
  15.  
  16. trace(uiColors.toXMLString());
  17.  
  18. /*outputs:
  19. <ui>
  20.   <default color="0xCCCCCC"/>
  21.   <background color="15724527"/>
  22.   <border color="0xff0000"/>
  23.   <button overColor="255" outColor="52428"/>
  24. </ui>
  25. */

The first time I needed to use an ActionScript variable within inline XML I was stumped. I couldn't figure it out and I wasn't able to easily find it on google. I eventually found it somewhere (don't remember where... possibly hidden away in the docs).

Now a search for "insert actionscript variables into e4x" on google gives plenty of results. But I figure it's worth a post.

I use actionscript to generate XML all the time so this comes in handy. I also store color values in automatically generated XML all the time. If I'm feeling organized I'll use something like what you see on line 10:

Actionscript:
  1. <border color={ ("0x" + borderColor.toString(16)) } />

If you look at the output you'll see this formats the uint so that it's readable as a hex number. By default (as you can see in the output) uints will show up in decimal notation. This really doesn't make any difference if you don't care about XML readability ... or if you just don't care about the readability of the colors stored in your XML.....

Also posted in color, variables | Tagged , , , | Leave a comment

E4X Filtering

Actionscript:
  1. var userInfo:XML = <users>
  2.   <user fname="joe" lname="smith" age="31" />
  3.   <user fname="mildred" lname="calder" age="64" />
  4.   <user fname="ben" lname="nathanson" age="20" />
  5.   <user fname="james" lname="biuford" age="19" />
  6.   <user fname="nick" lname="calhoun" age="45" />
  7. </users>;
  8.  
  9.  
  10. trace("Users over 20:\n");
  11. trace(userInfo.user.(@age> 20).toXMLString());
  12.  
  13. trace("\nUsers with the name nick:\n");
  14. trace(userInfo.user.(@fname == "nick" ).toXMLString());
  15.  
  16. // use regular expressions with e4x
  17. trace("\nUsers with name starting with j:\n");
  18. trace(userInfo.user.(/^j/.test(@fname)));
  19.  
  20. /*
  21. outputs:
  22.  
  23. Users over 20:
  24.  
  25. <user fname="joe" lname="smith" age="31"/>
  26. <user fname="mildred" lname="calder" age="64"/>
  27. <user fname="nick" lname="calhoun" age="45"/>
  28.  
  29. Users with the name nick:
  30.  
  31. <user fname="nick" lname="calhoun" age="45"/>
  32.  
  33. Users with name starting with j:
  34.  
  35. <user fname="joe" lname="smith" age="31"/>
  36. <user fname="james" lname="biuford" age="19"/>
  37.  
  38. */

One of the nicest features of E4X is filtering. The above code shows a few simple examples ... the last example makes use of regular expressions -- I first read using regular expressions and E4X somewhere on http://www.darronschall.com/.

I usually prefer to use a database for any kind of info I'll be searching... but if you know you have a relatively small amount of data XML can be a fine way to go.

Posted in XML | Tagged , , , , | 2 Comments

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