7 Replies Latest reply on May 26, 2009 7:06 AM by paraglidersd

    parsing XML array from httpservice result

    paraglidersd Level 1

      Hi All,

       

      I have banged my head against the wall when it comes to parsing XML data in flex.  This particular problem is probably an easy one (but not to me).  I have an XML document coming in to my flex app (via http service...pretty simple).  I took some examples from the Flex book by Gassner (chapter 21) as a guideline.  Some of the data in my xml struct is easy enough to parse (using those examples), but when I try to parse arrays of xml data (see below) I am having problems.  In my example, I am able to parse the 'inc_key' and pump it into an ArrayCollection.  Where I am having trouble is with the 'inc_data' tag and its children.  I have questions marks (??) next to the section in my mxml code where the problem is.  Can someone please point me in the right direction??????   I dont mind changing the structure of the xml to best fit reading it into flex structures.  Thanks, Bill

       

      data.xml
      <?xml version''1.0'?>
      <data>
        <inc_key>
          <key>20090515</key>
          <key>20090516</key>
          <key>20090517</key>
        </inc_key>
        <inc_data>
          <hr id='0'>100</hr>
          <hr id='1'>430</hr>
      ......
          <hr id='23'>160</hr>
        <inc_data>
        <inc_data>
          <hr id='0'>223</hr>
          <hr id='1'>678</hr>
          <hr id='2'>0</hr>
      ......
          <hr id='23'>860</hr>
      </inc_data>
        <inc_data>
          <hr id='0'>5</hr>
          <hr id='1'>34</hr>
          <hr id='2'>67</hr>
      ......
          <hr id='23'>10</hr>
      </inc_data>
      </data>

       

      MXML code
      .......
      private var contactService:HTTPService = new HTTPService();
      private function get_data():void
      {
         contactService.url="data.xml";
         contactService.addEventListener(ResultEvent.RESULT, resHandler);
         contactService.send();
      }

       

      private function resHandler(event:ResultEvent):void
      {
        var inc_array:ArrayCollection;
        inc_array = event.result.data.inc_key.key;
        Alert.show('inc 1 ' + inc_array.getItemAt(1));  // this will print out 20090516

       

        var data_array:ArrayCollection;
         // in this case, I have no idea what construct to use to get to the
         // arrays of hour data (hr).  I want an array of hr data within  inc_data
        data_array = event.result.data.inc_data.  ??????????

        • 1. Re: parsing XML array from httpservice result
          Michael Borbor Level 4

          You could use an XMLListCollection instead of looping and converting to an AC.

          • 2. Re: parsing XML array from httpservice result
            _Natasha_ Level 4

            Hi,

            var data_array:XMLList;
            data_array = event.result.data.inc_data.hr; // the collection of all child hr

            or

            data_array = event.result.data.inc_data.hr.text(); // the collection of all texts in hr

            • 3. Re: parsing XML array from httpservice result
              paraglidersd Level 1

              thanks for your responses, first and foremost.

              On the first response, I am an extreme newbie so could you provide an example using my data?  I would appreciate it.

              On the second response, neither of your suggestions worked.  When I tried the:

               

              var data_array:XMLList;

              data_array = event.result.data.inc_data.hr

              Alert.show(data.array.length();

               

              I first got an error at runtime (Type Coercion failed:  cannot convert mx.collections.ArrayCollection to XMLList).  So, I tried changing the second coding statement to:

               

              data_array = event.result.data.inc_data.hr as XMLList;

              Alert.show(data.array.length();

               

              and got an error at runtime that the data_array list was a null object.

               

              There is something fundamental I am missing, I think.  I basically have a collection with collection in my XML document and have no idea how to parse it.

               

              if I try to do the following:

               

              var a_collection:ArrayCollection;

              var b_collection:ArrayCollection;

              a_collection = event.result.data.inc_data.hr;

              a_collection = event.result.data.inc_data

               

              I receive an error that "hr" doesnt exist on a_collection. On b_collection, I can access it as an object, but I want to be able to access the 24 "hr" tags underneath that element.  I am pretty darn confused at this point.  ANY help is appreciated.

               

              thanks,

              Bill

              • 4. Re: parsing XML array from httpservice result
                paraglidersd Level 1

                Ok, through trial and error I realized one important thing I forgot which seemed to allow me to work with the content in the results paragraph.  I forgot to set the content type.  e.g.

                 

                private function get_data():void
                {
                   contactService.url="data.xml";

                   contactServce.contentType = "e4x";
                   contactService.addEventListener(ResultEvent.RESULT, resHandler);
                   contactService.send();
                }

                 

                I was able to parse the xml after that.

                • 5. Re: parsing XML array from httpservice result
                  paraglidersd Level 1

                  Ok, through trial and error I realized one important thing I forgot which seemed to allow me to work with the content in the results paragraph.  I forgot to set the content type.  e.g.

                   

                  private function get_data():void
                  {
                     contactService.url="data.xml";

                     contactServce.contentType = "e4x";
                     contactService.addEventListener(ResultEvent.RESULT, resHandler);
                     contactService.send();
                  }

                   

                  I was able to parse the xml after that.

                  • 6. Re: parsing XML array from httpservice result
                    paraglidersd Level 1

                    Ok, through trial and error I realized one important thing I forgot which seemed to allow me to work with the content in the results paragraph.  I forgot to set the content type.  e.g.

                     

                    private function get_data():void
                    {
                       contactService.url="data.xml";

                       contactServce.contentType = "e4x";
                       contactService.addEventListener(ResultEvent.RESULT, resHandler);
                       contactService.send();
                    }

                     

                    I was able to parse the xml after that.

                    • 7. Re: parsing XML array from httpservice result
                      paraglidersd Level 1

                      Ok, through trial and error I realized one important thing I forgot which seemed to allow me to work with the content in the results paragraph.  I forgot to set the content type.  e.g.

                       

                      private function get_data():void
                      {
                         contactService.url="data.xml";

                         contactServce.contentType = "e4x";
                         contactService.addEventListener(ResultEvent.RESULT, resHandler);
                         contactService.send();
                      }

                       

                      I was able to parse the xml after that.

                       

                      This solved it for me, for all intents and purposes.

                       

                      Message was edited by: paraglidersd