Tag Archives: XML

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.....

Posted in XML, color, variables | Also 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 | Also tagged , , , | 2 Comments

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.

Posted in XML, external data | Also tagged , , , , , | Leave a comment