11 Replies Latest reply on May 3, 2009 7:07 PM by Gregory Lafrance

    XML Parsing- Should be easy

    emorgen

      Hi,

       

      I'm trying to parse xml data from a website.  the xml code is as follows:

       

      <forecast>
      +
      <txt_forecast>
      <date>9:59 PM EDT</date>
      <number>2</number>
      −
      <forecastday>
      <period>1</period>
      <icon>nt_partlycloudy</icon>
      <title>Overnight</title>
      −
      <fcttext>
      Partly cloudy...then becoming mostly cloudy. Lows in the mid 50s. South winds around 5 mph late this evening...becoming light and variable.
      </fcttext>
      </forecastday>
      −
      <forecastday>
      <period>2</period>
      <icon>rain</icon>
      <title>Sunday</title>
      −
      <fcttext>
      Cloudy. A chance of showers in the morning...then showers likely in the afternoon. Highs in the lower 60s. Northeast winds around 5 mph...becoming southeast around 5 mph in the afternoon. Chance of rain 60 percent.
      </fcttext>
      </forecastday>
      </txt_forecast>
      −
      <simpleforecast>
      −
      <forecastday>
      <period>1</period>
      +
      <date>
      <epoch>1241406054</epoch>
      <pretty_short>11:00 PM EDT</pretty_short>
      <pretty>11:00 PM EDT on May 03, 2009</pretty>
      <day>3</day>
      <month>5</month>
      <year>2009</year>
      <yday>122</yday>
      <hour>23</hour>
      <min>00</min>
      <sec>54</sec>
      <isdst>1</isdst>
      <monthname>May</monthname>
      <weekday_short/>
      <weekday>Sunday</weekday>
      <ampm>PM</ampm>
      <tz_short>EDT</tz_short>
      <tz_long>America/New_York</tz_long>
      </date>
      −
      <high>
      <fahrenheit>59</fahrenheit>
      <celsius>15</celsius>
      </high>
      −
      <low>
      <fahrenheit>49</fahrenheit>
      <celsius>9</celsius>
      </low>
      <conditions>Rain Showers</conditions>
      <icon>rain</icon>
      <skyicon>cloudy</skyicon>
      <pop>59</pop>
      </forecastday>
      −
      <forecastday>
      <period>2</period>
      +
      <date>
      <epoch>1241492454</epoch>
      <pretty_short>11:00 PM EDT</pretty_short>
      <pretty>11:00 PM EDT on May 04, 2009</pretty>
      <day>4</day>
      <month>5</month>
      <year>2009</year>
      <yday>123</yday>
      <hour>23</hour>
      <min>00</min>
      <sec>54</sec>
      <isdst>1</isdst>
      <monthname>May</monthname>
      <weekday_short/>
      <weekday>Monday</weekday>
      <ampm>PM</ampm>
      <tz_short>EDT</tz_short>
      <tz_long>America/New_York</tz_long>
      </date>
      −
      <high>
      <fahrenheit>58</fahrenheit>
      <celsius>14</celsius>
      </high>
      −
      <low>
      <fahrenheit>49</fahrenheit>
      <celsius>9</celsius>
      </low>
      <conditions>Rain Showers</conditions>
      <icon>rain</icon>
      <skyicon>cloudy</skyicon>
      <pop>70</pop>
      </forecastday>
      

       

       

      I am trying to access the forecast.simpleforecast.forecastday information.

       

      The fields that I'm accessing are period, conditions, and high.fahrenheit.   I'm using a datagrid in flex and the code looks like this:

       

      <mx:DataGrid width="610" height="166.09999" dataProvider="{wunderground.lastResult.forecast.simpleforecast.forecastday}"  id="undergroundweather"   x="232" y="340.9">
                  <mx:columns>
                    <mx:DataGridColumn headerText="Day" dataField="period"/>
                    <mx:DataGridColumn headerText="High" dataField="high.fahrenheit"/>
                    <mx:DataGridColumn headerText="Low" dataField="low/fahrenheit"/>
                    <mx:DataGridColumn headerText="Condition" dataField="conditions"/>
                                 
                  </mx:columns>
                </mx:DataGrid>
      

       

      The period and conditions columns work but I can't get the high.fahrenheit to show any values.  If I remove the fahrenheit and leave it as high, it displays "[object Object]".

       

      Any idea what I'm doing wrong? Is there a special way to access subentries? 

       

      Thank you in advance!

       

      -Emorgen

        • 1. Re: XML Parsing- Should be easy
          ATIF FAROOQ Level 3

          hi,

             you can use itemRenderer to get nested Data . Data Field only takes

          Object as one Step down in hierarchy , because you are providing string in dataField not object

          so when you give such string 'high.fahrenheit'  , datagridColumn is Unable to parse your dataField because it tries to take

          'high.fahrenheit' as Single Object  . So alternative can be to use Custom

          ItemRenderer

           

          and with the renderer  you can set   yourLabel.text=data.high.fahrenheit

          • 2. Re: XML Parsing- Should be easy
            Gregory Lafrance Level 6

            One thing to watch out for, if you are parsing HTML data, it may not be well formed, and Flex may not like that. You may need to parse it on the server, extract the parts you are interested in, and wrap it so it is well formed "XML" before sending it back to Flex.

            • 3. Re: XML Parsing- Should be easy
              emorgen Level 1

              Can you explain the custom item renderer in more detail?  I do understand how to use it in this case.  I still want to display my data in a datagrid.  So if I use a itemRenderer what do I set dataField equal to?  Also, am I making a seperate itemRenderer object in flex or am I making it an attribute of the DataGridColumn?

              • 4. Re: XML Parsing- Should be easy
                Gregory Lafrance Level 6

                Your dataProvider establishes the base for the data. So myXLC is the data XMLLIstCollection or XMLList.

                 

                Your dataField indicates a level within the dataProvider, lilke myXMLItem.myElem.

                 

                Then within the itemRenderer you can go deeper in the XML structure like:

                 

                data.mySubLevel

                 

                which really means:

                 

                myXMLItem.myElem.mySubLevel

                • 5. Re: XML Parsing- Should be easy
                  emorgen Level 1

                  Thank you for your help Greg, but I am still confused on how I should be using the item renderer.

                   

                  I still want to keep my dataProvider as the same value.  I'm uncertain on how I should fill in my DataGridColumn for 'high' in my DataGrid.

                   

                  <mx:DataGrid width="610" height="166.09999" dataProvider="{wunderground.lastResult.forecast.simpleforecast.forecastday}"  id="undergroundweather"   x="232" y="340.9">
                              <mx:columns>
                                <mx:DataGridColumn headerText="Day" dataField="period"/>
                                <mx:DataGridColumn headerText="High" dataField="data.high.fahrenheit"/>
                                <mx:DataGridColumn headerText="Low"/>
                                <mx:DataGridColumn headerText="Condition" dataField="conditions"/>
                                             
                              </mx:columns>
                            </mx:DataGrid>
                  
                  • 6. Re: XML Parsing- Should be easy
                    Michael Borbor Level 4

                    Here are a few examlples that use item renderers, datagrids and XML

                     

                    http://www.adobe.com/devnet/flex/quickstart/using_item_renderers/

                    1 person found this helpful
                    • 7. Re: XML Parsing- Should be easy
                      Gregory Lafrance Level 6

                      You need to see the Flex builder help system docs on itemRenderers with datagrids and also the pages on e4x parsing syntax, then you need to play around with your data in the result handler, using e4x syntax and tracing out the result, then you can apply that knowledge to the datagridcolumn dataField and the data.xyz in the renderer.

                       

                      You can't necessarily say you want to keep what you currently have, you have to see if the syntax allows you to keep that, or if it must be modified based on the data and the syntax rules.

                      • 8. Re: XML Parsing- Should be easy
                        emorgen Level 1

                        After reading some other examples I was able to get it working.

                        My table now reads as follows:

                         

                         

                             <mx:DataGrid width="610" height="166.09999" dataProvider="{wunderground.lastResult.forecast.simpleforecast.forecastday}"  id="undergroundweather"   x="232" y="340.9">
                                    <mx:columns>
                                      <mx:DataGridColumn headerText="Day" dataField="period"/>
                                      <mx:DataGridColumn headerText="High" >
                                           <mx:itemRenderer >
                                                <mx:Component>
                                                     <mx:Label text="{data.high.fahrenheit}" />
                                                </mx:Component>
                                           </mx:itemRenderer>
                                      </mx:DataGridColumn>
                                      <mx:DataGridColumn headerText="Low" >
                                           <mx:itemRenderer >
                                                <mx:Component>
                                                     <mx:Label text="{data.low.fahrenheit}" />
                                                </mx:Component>
                                           </mx:itemRenderer>
                                      </mx:DataGridColumn>
                                      <mx:DataGridColumn headerText="Condition" dataField="conditions"/>
                                                   
                                    </mx:columns>
                                  </mx:DataGrid>
                        

                        Thank you all that have helped!

                        • 9. Re: XML Parsing- Should be easy
                          ntsiii Level 3

                          Another approach would be to use a labelFunction(oOtem:Object, dgc:):String.  It automatically gets a reference to the dataProvider item and to the dataGridColumn, and requires you build and return a string, which is displayed in the cell.

                           

                          In that function you can traverse the nested data in the item node.  Or access data in other "columns".  Whatever you need. Simpler thatn itreRenderer, but less flexible, it would be for when only the data needs to be dynamic.

                           

                          Also, tell me you have set resultFormat="e4x".

                           

                          Tracy Spratt

                          • 10. Re: XML Parsing- Should be easy
                            emorgen Level 1

                            I have not set the resultFormat for this HTTPService to e4x.

                            • 11. Re: XML Parsing- Should be easy
                              Gregory Lafrance Level 6

                              To parse this XML data more easily, the resultFormat should probably be e4x.