A Flash Developer Resource Site

Results 1 to 9 of 9

Thread: XML namespace and Attributes AGAIN!

  1. #1
    KoolMoves Moderator blanius's Avatar
    Join Date
    Jul 2001
    Location
    Atlanta GA
    Posts
    5,244

    XML namespace and Attributes AGAIN!

    Why can't APIs just use standard XML structure and not complicate things with attributes and namespaces.

    Anyway I'm once again struggling with a weather XML feed and namespaces.

    the XML looks like:
    Code:
    <aws:weather xmlns:aws="http://www.aws.com/aws">
      <aws:api version="2.0"/>
      <aws:WebURL>http://weather.weatherbug.com/GA/Marietta-weather.html?ZCode=Z5546&amp;Units=0&amp;stat=MRTTS</aws:WebURL>
      <aws:InputLocationURL>http://weather.weatherbug.com/GA/Marietta-weather.html?ZCode=Z5546&amp;Units=0</aws:InputLocationURL>
      <aws:ob>
        <aws:ob-date>
          <aws:year number="2009"/>
          <aws:month number="11" text="November" abbrv="Nov"/>
          <aws:day number="19" text="Thursday" abbrv="Thu"/>
          <aws:hour number="10" hour-24="22"/>
          <aws:minute number="10"/>
          <aws:second number="00"/>
          <aws:am-pm abbrv="PM"/>
          <aws:time-zone offset="-5" text="Eastern Standard Time" abbrv="EST"/>
        </aws:ob-date>
        <aws:requested-station-id/>
        <aws:station-id>MRTTS</aws:station-id>
        <aws:station>Tritt Elementary School Foundation</aws:station>
        <aws:city-state zipcode="30062">Marietta,  GA</aws:city-state>
        <aws:country>USA</aws:country>
        <aws:latitude>34.0172233581543</aws:latitude>
        <aws:longitude>-84.420280456543</aws:longitude>
        <aws:site-url>http://www.cobbk12.org/schools/es/schtritt/</aws:site-url>
        <aws:aux-temp units="&amp;deg;F">67</aws:aux-temp>
        <aws:aux-temp-rate units="&amp;deg;F">0</aws:aux-temp-rate>
        <aws:current-condition icon="http://deskwx.weatherbug.com/images/Forecast/icons/cond017.gif">Fair</aws:current-condition>
        <aws:dew-point units="&amp;deg;F">39</aws:dew-point>
        <aws:elevation units="ft">1050</aws:elevation>
        <aws:feels-like units="&amp;deg;F">47</aws:feels-like>
        <aws:gust-time>
          <aws:year number="2009"/>
          <aws:month number="11" text="November" abbrv="Nov"/>
          <aws:day number="19" text="Thursday" abbrv="Thu"/>
          <aws:hour number="12" hour-24="12"/>
          <aws:minute number="35"/>
          <aws:second number="00"/>
          <aws:am-pm abbrv="PM"/>
          <aws:time-zone offset="-5" text="Eastern Standard Time" abbrv="EST"/>
        </aws:gust-time>
        <aws:gust-direction>SW</aws:gust-direction>
        <aws:gust-speed units="mph">12</aws:gust-speed>
        <aws:humidity units="%">74</aws:humidity>
        <aws:humidity-high units="%">93</aws:humidity-high>
        <aws:humidity-low units="%">44</aws:humidity-low>
        <aws:humidity-rate>4</aws:humidity-rate>
        <aws:indoor-temp units="&amp;deg;F">78</aws:indoor-temp>
        <aws:indoor-temp-rate units="&amp;deg;F">0</aws:indoor-temp-rate>
        <aws:light>0.0</aws:light>
        <aws:light-rate>0.0</aws:light-rate>
        <aws:moon-phase moon-phase-img="http://api.wxbug.net/images/moonphase/mphase01.gif">-2</aws:moon-phase>
        <aws:pressure units="&quot;">30.14</aws:pressure>
        <aws:pressure-high units="&quot;">30.17</aws:pressure-high>
        <aws:pressure-low units="&quot;">30.10</aws:pressure-low>
        <aws:pressure-rate units="&quot;/h">0.00</aws:pressure-rate>
        <aws:rain-month units="&quot;">4.08</aws:rain-month>
        <aws:rain-rate units="&quot;/h">0.00</aws:rain-rate>
        <aws:rain-rate-max units="&quot;/h">0.00</aws:rain-rate-max>
        <aws:rain-today units="&quot;">0.00</aws:rain-today>
        <aws:rain-year units="&quot;">45.61</aws:rain-year>
        <aws:temp units="&amp;deg;F">47.2</aws:temp>
        <aws:temp-high units="&amp;deg;F">60</aws:temp-high>
        <aws:temp-low units="&amp;deg;F">39</aws:temp-low>
        <aws:temp-rate units="&amp;deg;F/h">-1.6</aws:temp-rate>
        <aws:sunrise>
          <aws:year number="2009"/>
          <aws:month number="11" text="November" abbrv="Nov"/>
          <aws:day number="19" text="Thursday" abbrv="Thu"/>
          <aws:hour number="7" hour-24="07"/>
          <aws:minute number="13"/>
          <aws:second number="49"/>
          <aws:am-pm abbrv="AM"/>
          <aws:time-zone offset="-5" text="Eastern Standard Time" abbrv="EST"/>
        </aws:sunrise>
        <aws:sunset>
          <aws:year number="2009"/>
          <aws:month number="11" text="November" abbrv="Nov"/>
          <aws:day number="19" text="Thursday" abbrv="Thu"/>
          <aws:hour number="5" hour-24="17"/>
          <aws:minute number="32"/>
          <aws:second number="02"/>
          <aws:am-pm abbrv="PM"/>
          <aws:time-zone offset="-5" text="Eastern Standard Time" abbrv="EST"/>
        </aws:sunset>
        <aws:wet-bulb units="&amp;deg;F">43.502</aws:wet-bulb>
        <aws:wind-speed units="mph">0</aws:wind-speed>
        <aws:wind-speed-avg units="mph">2</aws:wind-speed-avg>
        <aws:wind-direction>WNW</aws:wind-direction>
        <aws:wind-direction-avg>WNW</aws:wind-direction-avg>
      </aws:ob>
      <ignoreWhitespace>true</ignoreWhitespace>
    </aws:weather>
    I've found an example at http://animateinexile.blogspot.com/2...on-in-xml.html

    but when I try this I get an error that AS is expecting a variable at that line(xmlData..bug::["temp-high"]);).

    PHP Code:
    function loadXML(e:Event){
        var 
    xmlData:XML = new XML(e.target.data);
        
    xmlData.ignoreWhitespace=true;
        var 
    bug:Namespace = new Namespace("http://www.aws.com/aws");
        
    trace(xmlData..bug::["temp-high"]);
        
    //trace(xmlData);
        
    }
    function 
    io_error(e:Event){
        
    trace("error");
        } 

  2. #2
    up to my .as in code Chris_Seahorn's Avatar
    Join Date
    Dec 2004
    Posts
    4,389
    I don't believe Koolmoves supports the operator "::" for qualifying namespace names (could be wrong).

    I beleve one of the reasons WIlbert posted his busting XML without E4X example a while ago was to help users bust down advanced xml structures without need of E4X (to avoid syntax issues for properties, operators or commands we may not support yet)....again I could be wrong

    That said, and using his suggestion....your feed targeting the temp-high node could be handled like so:

    function xmlLoaded(e:Event):void {

    var xml:XML = new XML(e.target.data);
    var weatherbug:Namespace = new Namespace('http://www.aws.com/aws');
    var temphigh:QName = new QName(weatherbug, 'temp-high');
    var temphighlist:XMLList = xml.descendants(temphigh);

    trace (temphighlist[0]);


    }

  3. #3
    KoolMoves Moderator blanius's Avatar
    Join Date
    Jul 2001
    Location
    Atlanta GA
    Posts
    5,244
    Ugh! As usual you are way ahead of me. I'll try to decipher that code!
    I need to understand it to really make good use of it.

    What I see in the XML for example are lines like
    <aws:city-state zipcode="30062">Marietta, GA</aws:city-state>

    I may need zipcode or I may want the text in the node, I would really need a straigt forward way to look at that line and know how to get each as needed. I would like to load several XML data files like this one. I understand that the zipcode="30062" is an attribute and the Marietta, GA is the text node. Looking at your example I'll need a Qname for each and every node? that can't be right.

    Again nodes like
    <aws:feels-like units="&amp;deg;F">47</aws:feels-like>
    I'll want the units attribute and the text...

    This feels overly complex and frankly I'd be better off just doing string searchs at least I'd understand what I was doing.

  4. #4
    KoolMoves Moderator blanius's Avatar
    Join Date
    Jul 2001
    Location
    Atlanta GA
    Posts
    5,244
    Ok re-reading Wilberts post and I see where I need to go. Sigh.... At least it will work.

  5. #5
    KoolMoves Moderator blanius's Avatar
    Join Date
    Jul 2001
    Location
    Atlanta GA
    Posts
    5,244
    Thanks Chris. I've got it going at least, just alot of code for each bit of data for example
    Code:
    	var xml:XML = new XML(e.target.data);
    
    	var bug_weather:Namespace = new Namespace('http://www.aws.com/aws');
    	
    	var qn_location:	QName = new QName(bug_weather, 'city-state');
    	var qn_temp:		QName = new QName(bug_weather, 'aux-temp');	
    	var qn_sunrise:		QName = new QName(bug_weather, 'sunrise');
    	var qn_condition:  	QName = new QName(bug_weather, 'current-condition');
    	
    	var location	        :XMLList		= xml.descendants(qn_location);
    	var temp		:XMLList 		= xml.descendants(qn_temp);
    	var sunrise		:XMLList		= xml.descendants(qn_sunrise);
    	var condition	:XMLList		= xml.descendants(qn_condition);
    	
    	var city		:String = location;
    	var zip		:String = location.attribute('zipcode');
    	var degrees		:String = temp.attribute('units');
    	var temerature	:String = temp;
    	var currently	:String = condition;
    	var currently_icon:URLRequest = new URLRequest(condition.attribute('icon'));
    It really feels like I'm going "Around the Barn" to get where I want.

  6. #6
    up to my .as in code Chris_Seahorn's Avatar
    Join Date
    Dec 2004
    Posts
    4,389
    This example is specific to your need, not all needs. You always analize the rss feed in question and decide best how to target what you want in the least amount of code. This example disregards the "sunrise" section because although your posted code previously references it, it does not seem to use it (nor does mine).

    That said I would go about it this way. It assumes either one text node or one attriute per Nodeame so bear in mind it would need to be reworked or altered for RSS structures using multiple attributes. Try this:

    function xmlLoaded(e:Event):void {
    xml= new XML(e.target.data);

    var city:String = findNode("city-state",false);
    var zip:String = findNode("city-state",true);
    var degrees:String = findNode("aux-temp",true);
    var temperature:String = findNode("aux-temp",false);
    var currently:String = findNode("current-condition",false);
    var currently_icon:URLRequest = new URLRequest(findNode("current-condition",true));
    //trace to ensure chris is not nuts...then delete these lines as they are waste
    trace(city);
    trace(zip);
    trace(degrees);
    trace(temperature);
    trace(currently);
    trace(findNode("current-condition",true));

    }

    function findNode(e:String,isAtt:Boolean):String{

    var weatherbug:Namespace = new Namespace('http://www.aws.com/aws');
    var searchNode:QName = new QName(weatherbug, e);
    var nodeList:XMLList = xml.descendants(searchNode);

    if (isAtt) return nodeList.@*;
    else return nodeList;


    }

  7. #7
    KoolMoves Moderator blanius's Avatar
    Join Date
    Jul 2001
    Location
    Atlanta GA
    Posts
    5,244
    Ah now that is more like it, thanks tons Chris (and you are nuts by the way, but good nuts LOL)

    I'll probably use the sunrise node as well but I'll deal with that separately.

  8. #8
    KoolMoves Moderator blanius's Avatar
    Join Date
    Jul 2001
    Location
    Atlanta GA
    Posts
    5,244
    By the way I'm bumping this as the latest version of Koolmoves now handles the node syntax as show above:
    PHP Code:
    function loadXML(e:Event){
        var 
    xmlData:XML = new XML(e.target.data);
        
    xmlData.ignoreWhitespace=true;
        var 
    bug:Namespace = new Namespace("http://www.aws.com/aws");
        
    trace(xmlData..bug::["temp-high"]);
        
    //trace(xmlData);
        
    }
    function 
    io_error(e:Event){
        
    trace("error");
        } 
    And as a result I'm well on the way to getting a project done with WeatherBug Data

  9. #9

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  




Click Here to Expand Forum to Full Width

HTML5 Development Center