5 Replies Latest reply on Jan 15, 2010 2:19 AM by harsh_hot

    how to iterate over a tree

    harsh_hot

      Hi friends,

       

      I am new to flex and hence need your help in a problem.I am getting an xmllist through httpservice and now i want to iterate over this list so as to access individual nodes but i am unable to figure out a way. Please suggest me how do i iterate over nodes using httpservice and xmllistcollection.

        • 1. Re: how to iterate over a tree
          Gregory Lafrance Level 6

          After you get the data back from your HTTPService (set its resultFormat property to e4x), use e4x syntax to access the data.

           

          If this is your data myXML:

           

          <items>

              <item>

                  <name>test1</name>

                  <addr>test address1</addr>

              </item>

              <item>

                  <name>test2</name>

                  <addr>test address2</addr>

              </item>

              <item>

                  <name>test3</name>

                  <addr>test address3</addr>

              </item>

          </items>

           

          then you can access the data and assign to a data provider with:

           

          myXmlListCollection = new XMLListCollection(myXML..item);

           

          You can loop through the data lie this:

           

          for each(var xml:XML in myXML..item){

            trace(xml.toXMLString());

          }

           

          http://livedocs.adobe.com/flex/3/html/help.html?content=13_Working_with_XML_03.html

           

          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

          • 2. Re: how to iterate over a tree
            harsh_hot Level 1

            Thanks Greg for the reply but it could not solve the problem.Actually i am reading the file from http service and then storing the result in xmllistcollection. Now i have to iterate over this xmllistcollection rather than over the tree.PFB the code.

             

            <mx:HTTPService id="httpid" url="org.xml" resultFormat="e4x"/>

             

            <mx:XMLListCollection id="xmllistid" source="{httpid.lastResult.person}"/>

             

            I am trying to do something like this but its not working

             

            for each(var xml:XML in xmllistid){
               
                Alert.show(xml.toString());
               
                }

             

            Please suggest.

            • 3. Re: how to iterate over a tree
              Gregory Lafrance Level 6

              If you have verified that you have data coming back from the server, maybe this will work:

               

              Alert.show(xml.toXMLString());

              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: how to iterate over a tree
                harsh_hot Level 1

                Thanks Greg,

                 

                I tried doing that too but of no avail. I am reading from an external file "org.xml" stored locally, via httpservice and then storing it in xmllistcollection.PFB my code

                 

                <mx:HTTPService id="httpid" url="org.xml" resultFormat="e4x"/>

                 

                <mx:XMLListCollection id="xmllistid" source="{httpid.lastResult.person}"/>

                 

                for each(var xml:XML in xmllistid){
                   
                    Alert.show(xml.toXMLString());
                   
                    }

                 

                But it doesnt display anything. Whereas, when i use a <mx:Tree> element it displays the tree perfectly using the following code

                 

                <mx:Tree x="731" y="10" width="198" height="480" id="sampletree" dataProvider="{xmllistid}" labelField="@name"></mx:Tree>

                 

                So it means that data is coming from httpservice and getting stored in xmllistcollection but while traversing it i am getting problems.

                • 5. Re: how to iterate over a tree
                  harsh_hot Level 1

                  I used URLRequest and URLLoader and the problem got solved. I stored the result retrieved from it in a XML variable and then iterated over it.