9 Replies Latest reply on Jun 9, 2007 4:58 AM by thedex1

    Problem in reading data from XML

    thedex1 Level 1
      Hi,
      I have very confusing problem:
      I have the following XML structure:

      <catalog>
      <category>
      <id_cat>1</id_cat>
      <label_cat>Movies</label_cat>
      <item>
      <id>1</id>
      <label>Are We There Yet?</label>
      <toolTipImg></toolTipImg>
      </item>
      <item>
      <id>2</id>
      <label>The messenger</label>
      <toolTipImg></toolTipImg>
      </item>
      </category>
      <category>
      <id_cat>2</id_cat>
      <label_cat>Mobile Phone</label_cat>
      <item>
      <id>1</id>
      <label>Nokia</label>
      <toolTipImg></toolTipImg>
      </item>
      </category>
      </catalog>

      The above XML contains:
      No of Categories = 2
      No of items in first Category = 2
      No of items in second Category = 1

      I created Accordion container. To add data to it, I created two functions. One of the functions is used to get Categories and the other function to ad item to the category. My problem is that when I execute my application, it fill the conainer with category having more than one item in it but leave the category empty having only one item.
      Here are the functions:

      public function RSSResultHandler(event:ResultEvent):void
      {
      resultData = event.result.catalog is ArrayCollection?event.result.catalog as ArrayCollection:new ArrayCollection(ArrayUtil.toArray(event.result.catalog));
      menuCreator(event);
      }

      public function menuCreator(event:ResultEvent):void
      {
      var lenCat:int = event.result.catalog.category.length; // Category Length
      for(var i:int = 0 ; i < lenCat ; i++)
      {

      menuItem = new VBox();
      menuItem.id = "menuItem"+event.result.catalog.category .id_cat;
      menuItem.label = event.result.catalog.category
      .label_cat;
      var iParam:String = i.toString();
      var temp:String = menuItemFiller(event,iParam,menuItem);
      }
      }
      public function menuItemFiller(event:ResultEvent,idCat:String,object:Object):String
      {
      var lenCatItems:int = event.result.catalog.category[idCat].item.length; // Number of items in category
      //Alert.show(event.result.catalog.category[2].item.length.toString());
      var j:int=0;
      while(j < lenCatItems)
      {
      var catItem:LinkButton = new LinkButton();
      catItem.id = "catItem"+event.result.catalog.category[idCat].item[j].id;
      catItem.label = event.result.catalog.category[idCat].item[j].label;
      menuItem.addChild(catItem);
      j++
      }
      menu.addChild(menuItem);
      return "bye";
      }

      I am really confused with this problem. Although it is simple but for me, it is more than simple since I cannot figure out the problem. Any help, suggestion will be really really apprectiated.

      Thanks...

      Best Regards
      DeX
        • 1. Re: amazing problem..! ! !
          thedex1 Level 1
          In general, can you please let me know what are the possible reasons when the application display XML data properly when it contains more then entries and displays blank when it contains only one entries.

          I shall be very thankful to you.

          Best regards
          DeX
          • 2. Re: Problem in reading data from XML
            Chemical_One
            i am unsure if i understand your issue if you would not want to display anythig when it has only one item the the length property should be useful something likke
            if(myarray.length > 1){
            mydataprovider = myarray;
            }
            else{
            mydataprovider = "";
            }
            I have found it to be easier when dealng with remote xml to use the following syntax
            xmldata.lastResult..category.item
            but again i am not sure i understand your problem
            : )
            • 3. Re: Problem in reading data from XML
              thedex1 Level 1
              Dear Chemical,


              Thank you for your reply. Let me explain what is the problem.

              <catalog>
              <category>

              <item>
              </item>

              </category>

              </catalog>

              Now, you can see that when I try to get length of item in category, it should give me 1. That is, category[0].item.length = 1 but my application gives 0

              If I increase number of item in category, like this:


              <catalog>
              <category>

              <item>
              </item>

              <item>
              </item>

              </category>

              </catalog>

              Then my application give correct "item" length...that is 2

              My question is why it is giving me ZERO length when I have 1 item in category???

              Functions I am using can be found above.

              Thanks.

              Regards
              DeX
              • 4. Re: Problem in reading data from XML
                thedex1 Level 1
                Hi,

                How are you? I have very frustrating problem with Flex. I really need help with this. I am sending you this question with the hope that the problem will be resolved:

                I am sending you two things:
                1)- Functions
                2)- XML Sample Data

                Here is my function:
                public var menuItem:VBox;
                public var menu:Accordion = new Accordion();

                public function RSS():void
                {
                service = new HTTPService("");
                service.url = "data/dataStore.xml";
                service.addEventListener(ResultEvent.RESULT, RSSResultHandler);
                service.send();
                }

                public function RSSResultHandler(event:ResultEvent):void
                {
                resultData = event.result.catalog is ArrayCollection?event.result.catalog as ArrayCollection:new ArrayCollection(ArrayUtil.toArray(event.result.catalog));
                menuCreator(event);
                }

                public function menuCreator(event:ResultEvent):void
                {
                var lenCat:int = event.result.catalog.category.length; // Category Length
                for(var i:int = 0 ; i < lenCat ; i++)
                {
                menuItem = new VBox();
                menuItem.label = event.result.catalog.category.label_cat;
                var iParam:String = i.toString();
                var temp:String = menuItemFiller(event,iParam,menuItem);
                }
                }

                public function menuItemFiller(event:ResultEvent,idCat:String,object:Object):String
                {
                var lenCatItems:int = event.result.catalog.category[idCat].item.length; // Number of items in category
                var j:int=0;
                while(j < lenCatItems)
                {
                var catItem:LinkButton = new LinkButton();
                catItem.label = event.result.catalog.category[idCat].item[j].label;
                menuItem.addChild(catItem);
                j++
                }
                menu.addChild(menuItem);
                return "bye";
                }

                And here is my XML Data. I use to store this data in "Data\dataStore.xml":

                <catalog>
                <category>
                <label_cat>Movies</label_cat>

                <item>
                <label>Are We There Yet?</label>
                </item>

                <item>
                <label>The messenger</label>
                </item>
                </category>

                <category>
                <label_cat>Mobile Phone</label_cat>

                <item>
                <label>Nokia</label>
                </item>
                </category>
                </catalog>

                Now here is my simple problem:

                You can see that:
                Catalog contains -> 2 category elements
                First Category contains -> 2 item elements
                Second Category contains -> i item elements

                Now when I try to get:
                " event.result.catalog.category[0].item.length; ", it gives me 2 but when I try to get " event.result.catalog.category[1].item.length; ", it gives me ZERO even there is an item tag inside second category.

                WHAT MY FUNCTIONS DO:
                I am just trying to create an Accordian and fill it using XML data

                I am really confused with this. Any help or suggestion will be realy realy appreciated.
                I shall be very thank full to you.

                Best Regards
                DeX
                • 5. Re: Problem in reading data from XML
                  dazweeja
                  Have you tried putting id attributes in your XML so that Flex has a better chance of guessing the format (as there isn't a Document Type Definition for this XML). I haven't tested this but maybe try:

                  <catalog>
                  <category id='1'>
                  <label_cat>Movies</label_cat>

                  <item id='1'>
                  <label>Are We There Yet?</label>
                  </item>

                  <item id='2'>
                  <label>The messenger</label>
                  </item>
                  </category>

                  <category id='2'>
                  <label_cat>Mobile Phone</label_cat>

                  <item id='1'>
                  <label>Nokia</label>
                  </item>
                  </category>
                  </catalog>
                  • 6. Re: Problem in reading data from XML
                    dazweeja Level 1
                    Actually, I think the solution is to use XMLList rather than ArrayCollection:

                    http://tech.groups.yahoo.com/group/flexcoders/message/76954

                    Cheers,
                    Daz.
                    • 7. Re: Problem in reading data from XML
                      thedex1 Level 1
                      Hi,

                      Thank you all for replying me. Actually when I try to use XMLList object as:

                      var xml:XMLList = new XMLList(service.lastResult);
                      Alert.show(xml.catalog.category[0].label_cat.toString());

                      It gives me following error:
                      TypeError: Error #1010: A term is undefined and has no properties.

                      And also when I try to use like this:

                      Alert.show(service.lastResult.catalog.category[0].item.(id == 1).label.toString());

                      it gives me following error:
                      TypeError: Error #1123: Filter operator not supported on type mx.collections.ArrayCollection.

                      Thanks for your help....

                      Thanks,

                      DeX
                      • 8. Re: Problem in reading data from XML
                        dazweeja Level 1
                        The following simple example works with your XML file:

                        <?xml version="1.0" encoding="utf-8"?>
                        <mx:Application xmlns:mx=" http://www.adobe.com/2006/mxml" layout="absolute" initialize="initializeHandler();">
                        <mx:Script>

                        <![CDATA[
                        [Bindable]
                        public var tempLabelCat:XMLList;

                        [Bindable]
                        public var thisXML:XML =
                        <catalog>
                        <category>
                        <label_cat>Movies</label_cat>

                        <item>
                        <label>Are We There Yet?</label>
                        </item>

                        <item>
                        <label>The messenger</label>
                        </item>
                        </category>

                        <category>
                        <label_cat>Mobile Phone</label_cat>

                        <item>
                        <label>Nokia</label>
                        </item>
                        </category>
                        </catalog>

                        private function initializeHandler():void
                        {
                        tempLabelCat = thisXML.category[1].label_cat;
                        }
                        ]]>
                        </mx:Script>
                        <mx:Label text="{'tempLabelCat: ' + tempLabelCat}"/>
                        </mx:Application>

                        A couple of points to note. Firstly, you don't need the overall container 'catalog' when referring to the individual categories, ie. thisXML.category[1].label_cat is correct. Secondly, the object containing your XML file will be of type XML, it is the XML fragments that are of type XMLList. In your case, when using HTTPService, you might use something like this:

                        [Bindable]
                        private var thisXML:XML;

                        private function RSSResultHandler(event:ResultEvent):void
                        {
                        thisXML = event.result as XML;
                        }

                        Finally, have a look at this page:
                        http://www.adobe.com/devnet/flex/quickstart/httpservice/

                        Good luck,
                        Daz.
                        • 9. Re: Problem in reading data from XML
                          thedex1 Level 1
                          Thank you , thank you so much for your help.
                          Huge bundles of thanks from me

                          Thanks,

                          DeX