2 Replies Latest reply on Jun 26, 2010 8:19 AM by jsd99

    HTTPService XML: force ArrayCollections?

    jsd99 Level 3

      I'm loading an XML document with HTTPService.  It has an annoying behavior.  If the source XML looks like:

       

      <one>

        <two>thing</two>

      </one>

       

      then one.two will be a String ("thing").

       

      If the XML is:

       

      <one>

         <two>thing</two>

         <two>another thing</two>

      </one>

       

      then one.two will be an ArrayCollection.

       

      Is there a way to force the first case to also generate an ArrayCollection?  Right now I have to manually check everywhere if two is an ArrayCollection, if it isn't, make a new one, etc... Seems like Flex should take care of this automatically.

       

      -jsd-

        • 1. Re: HTTPService XML: force ArrayCollections?
          BhaskerChari Level 4

          Hi,

           

          Checkout the below code...this will ensure that you will get only ArrayCollection in both the cases...and also you need to check only once whether it is arrycollection or not and at other places you can avoid the check as you are converting it to ArrayCollection so you can directly use the arraycollection..

           

          <?xml version="1.0" encoding="utf-8"?>
          <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" creationComplete="loadXMLData();" layout="absolute">
          <mx:Script>
            <![CDATA[
             import mx.collections.ArrayCollection;
             import mx.rpc.xml.SimpleXMLDecoder;
            
             private var arrayCollection:ArrayCollection = new ArrayCollection();
            
             private function loadXMLData():void
             {
              var xml:XML = new XML(myXml.toString());
              var xmlDoc:XMLDocument = new XMLDocument(xml);
              var decoder:SimpleXMLDecoder = new SimpleXMLDecoder(true);
              var resultObj:Object = decoder.decodeXML(xmlDoc);
             
              if(resultObj != null)
              {
               if(resultObj.one.two is ArrayCollection)
               {
                arrayCollection = resultObj.one.two as ArrayCollection;
               }
               else if(resultObj.one.two is String)
               {
                arrayCollection.addItem(resultObj.one.two);
               }
              }
             }
            
            ]]>
          </mx:Script>
          <mx:XML id="myXml" xmlns="">
            <one>
              <two>thing</two>
            </one>
          </mx:XML>

          </mx:Application>


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


          Thanks,

          Bhasker Chari

          • 2. Re: HTTPService XML: force ArrayCollections?
            jsd99 Level 3

            Thanks for the reply.  I am already doing that, but it's horribly inconvenient because there are many many places where I have to have the same convoluted logic.  There should be a global option when converting XML to always make Arrays.