4 Replies Latest reply on Feb 22, 2010 9:07 AM by leoserra

    XML access to atributes and tag values at the same time

    leoserra Level 2

      Hi to everyone,

       

      I'm making some experiences with Flex, but I already have some experience with Flash and AS3.

       

      What I'm trying to accomplish, is to make a small application that uses webservices from the Weather Channel. The first part of the application is to ask the user his location. Then the location is sent to the webservice and the system return an XML with the possible matches. The user chooses one, and the weather information is shown.

       

      I have no problem with the navigation, or passing the results from one stage to another, but I'm having a problem, that I think it has an easy fix, but I can't get a light or answer from what I've seen from these forums or webasites... I suspect it's because the format of the XML that is return.

       

      For example, my request is this one: http://xoap.weather.com/search/search?where=aveiro

       

      wich return two possible locations, in this way:

      <search ver="3.0">
           <loc id="BRXX1094" type="1">Aveiro, Brazil</loc>
           <loc id="POXX0006" type="1">Aveiro, Portugal</loc>
      </search>

       

      Well, I want to display in a Datagrid the "id" attribute, and the "loc" tag value, i.e, "Aveiro, Brazil" and "Aveiro, Portugal".

       

      I defined the DataGrid like this:

      <mx:DataGrid x="10" y="53" width="365" id="dgLocation" dataProvider="{wSearch.lastResult.loc}" itemClick="callService(event);">
            <mx:columns>
                   <mx:DataGridColumn headerText="Localidade" dataField="loc"/>
                   <mx:DataGridColumn headerText="Referência" dataField="@id"/>
            </mx:columns>
      </mx:DataGrid>

       

      In the "loc" dataField, doesn't show anything, and the "@id" does display the correct information. Since I only have "loc" tag inside the main root tag, how can I get teir values? With labelFunction? How would you write it to get the information?

       

      This is my last try:

      private function getLocationName(item:Object, column:DataGridColumn):String{
             var xmlitem:XML=XML(item);
             trace(xmlitem.loc.toXMLString());
             return item.loc;
      }

       

      Any ideias are welcome. Thanks.

        • 1. Re: XML access to atributes and tag values at the same time
          P.o.l.a.c.o Level 1

          Have you tried

          <mx:DataGridColumn headerText="Localidade" dataField="text"/>

          or

          <mx:DataGridColumn headerText="Localidade" dataField="text()"/>

          ?

          • 2. Re: XML access to atributes and tag values at the same time
            leoserra Level 2

            Hi, P.o.l.a.c.o

             

            I was so focused in other approach I didn't consider the "text" method, I use a lot in AS3 , but in this case is not helpfull.

             

            I ran this in debugging mode and this warning was dispatched: "warning: unable to bind to property 'loc' on class 'XML' (class is not an IEventDispatcher)", which comes from here:

            <mx:DataGrid x="10" y="53" width="365" id="dgLocation" dataProvider="{wSearch.lastResult.loc}" itemClick="callService(event);">

             

            But, I can still get the attributes, but not the tag value.

            • 3. Re: XML access to atributes and tag values at the same time
              Gregory Lafrance Level 6

              This seems to work:

               

              <?xml version="1.0" encoding="utf-8"?>
              <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml"
                creationComplete="wSearch.send();">
                <mx:Script>
                  <![CDATA[
                    import mx.controls.dataGridClasses.DataGridColumn;
                    
                    private function getData(item:Object, column:DataGridColumn):String{
                      return XML(item).valueOf();
                    }
                  ]]>
                </mx:Script>
                <mx:HTTPService id="wSearch" url="http://xoap.weather.com/search/search?where=aveiro"
                  resultFormat="e4x"/>
                <mx:DataGrid x="10" y="53" width="365" id="dgLocation" 
                  dataProvider="{wSearch.lastResult.loc}">
                  <mx:columns>
                    <mx:DataGridColumn headerText="Localidade" labelFunction="getData"/>
                    <mx:DataGridColumn headerText="Referência" dataField="@id"/>
                  </mx:columns>
                </mx:DataGrid>
              </mx:Application>
              
              


              If this post answers your question or helps, please mark it as such.


              Greg Lafrance - Flex 2 and 3 ACE certified

              www.ChikaraDev.com

              Flex / AIR Development, Training, and Support Services

              • 4. Re: XML access to atributes and tag values at the same time
                leoserra Level 2

                Thank you very much Greg, I knew that it would be a simple thing, but never crossed the valueOf method.

                 

                It's solved.