Here is some untested code to loop (you will need to correct the automatic
capitalization ): It is also more verbose than necessary, just for clarity.
Var xmlResult:XML = event.result as XML;
Trace(xmlResult.toXMLString()); //to verify your xml
Var xlResult:XMLList = xmlResult.category;
Var acResult:ArrayCollection = new ArrayCollection();
Var xmlCategory:XML;
For (var i:uint=0;i<xlResult.length()) { //watch out for the length() method,
not property
xmlCategory = xlResult[i];
acResult.addItem({id:xmlCategory.id.text(),label:xmlCategory.label.text(),icon:xmlCategory.icon.text()});
//usually the text) can be omitted, but I use it by habit.
}
myTileList.dataProvider = acResult;
And DO avoid using lastResult in functions. It is intended for binding
espressions.
Tracy Spratt,
Lariat Services, development services available
_____
From: [email protected] [mailto:[email protected]] On Behalf
Of Tracy Spratt
Sent: Tuesday, April 20, 2010 12:39 PM
To: [email protected]
Subject: RE: [flexcoders] Re: Problems With my XML Created Array Collection
I know this is very late in the game, and I haven’t read the full thread, but
this seems like a very complicated solution to the problem. Further, the
default resultFormat has other consequences, data type conversions for example.
If you have a string that looks like a number, it will get converted to a
number, removing trailing and leading zeros and such, and sometimes this is not
desired.
Using e4x and looping over the categories.category XMLList is very simple.
XMLLists are never null, but empty nodes just result in a zero length, which is
easy to test for.
Tracy Spratt
_____
From: [email protected] [mailto:[email protected]] On Behalf
Of James
Sent: Monday, April 19, 2010 3:39 PM
To: [email protected]
Subject: [flexcoders] Re: Problems With my XML Created Array Collection
I've noticed that if I remove all items from the mysql table which populates
the remote xml file the xml that is produced is simply this:-
<?xml version="1.0" ?>
<categories />
i.e. all of the nodes are removed so this means my app is looking for nodes
that aren't actually there so it seems I need another else if bracket to deal
with this but I haven't got a clue for how to check if an xml file is empty or
null or whatever you'd call the above in flex and what to do within this else
if bracket if it's not.
--- In flexcod...@yahoogro <mailto:flexcoders%40yahoogroups.com> ups.com,
Robert Moss <rdm0...@...> wrote:
>
> In essence yes, I do not think I would do it as part of the button click, but
> instead clear it before the IF in the result handler with LinksFullAC.source
> = new Array(); or LinksFullAC.removeAll(); I prefer the former.
>
> As for your error, I'm not sure what's happening, but you should be able to
> set a break in the result handler and see what is getting returned. It looks
> like for the combination you describe, categories is not getting returned for
> some reason. You'll need to figure out why and trap for it. The same goes for
> returning 0 records. See what is getting returned and trap for it.
>
>
>
>
>
> ________________________________
> From: James <garymoorcroft_...@...>
> To: flexcod...@yahoogro <mailto:flexcoders%40yahoogroups.com> ups.com
> Sent: Mon, April 19, 2010 12:47:15 PM
> Subject: [flexcoders] Re: Problems With my XML Created Array Collection
>
>
> Hi Robert. Thanks so much for that my friend.
>
> I have some problems with it though. You see I have an event in my app
> applied to a button (refresh button) which resends data via the LinksService
> i.e. LinksService. send and I've noticed that with your code if there is only
> 1 link i.e. 1 node and I click the refresh button it seems to keep recreating
> that 1 over and over again as if it's recreating the same object in the array
> collection or something. This doesn't happen if their are 2 or more
> links/nodes/ objects. Perhaps I'll have to get the refresh button to empty
> the array collection first before sending the data i.e. it's click function
> would be "LinksFullAC. flush; LinksService. send()" Am I right?
>
> I've also noticed now with your code applied if I select an item in the
> tilelist and click the reresh button I get the error "Property categories not
> found on String and there is no default value" which is indicating the error
> at the if statement in the code you gave me and the LinksService itself. Any
> idea what's causing this?
>
> Another question though what if the xml contains no nodes?
>
> You see my xml file is dynamic, I have a variable in my app called categoryid
> and that variable is based on a category that the user selects in my app and
> is submitted to getlinks.php which gets all the records from my mysql table
> which contain that specific submitted categoryid and output them as the xml
> file. Some categoryids have no records (the user can add and delte records
> via my app). Is there any way of getting it to handle 0 records/nodes as well
> as handling muliple records/1record which it seems to already do?
>
> Thanks so much for your help. Sorry to be a pain :-(
>
> --- In flexcod...@yahoogro ups.com, Robert Moss <rdm0004@ > wrote:
> >
> > This is not exactly how I would implement, but you should get the idea. If
> > you have multiple category nodes, then your LinksService. lastResult.
> > categories. category is returned as an ArrayCollection and your code works.
> > If it only has one node then LinksService. lastResult. categories. category
> > is returned as an ObjectProxy. So in your return method you could have the
> > following.
> >
> > if(LinksService. lastResult. categories. category is ArrayCollection) {
> > LinksFullAC= LinksService. lastResult. categories. category as
> > ArrayCollection;
> > }else
> > {
> > var oCat:Object = new Object();
> > oCat.id = LinksService. lastResult. categories. category. id;
> > oCat.label = LinksService. lastResult. categories. category. label;
> > oCat.icon = LinksService. lastResult. categories. category. icon;
> > LinksFullAC. addItem(oCat) ; // NOTE: LinksFullAC needs to not only be
> > previously declared, but must also me instantiated with new
> > ArrayCollection( ) for this line to work.
> >
> > }
> >
> >
> >
> >
> >
> > ____________ _________ _________ __
> > From: James <garymoorcroft_ ict@>
> > To: flexcod...@yahoogro ups.com
> > Sent: Mon, April 19, 2010 9:58:33 AM
> > Subject: [flexcoders] Re: Problems With my XML Created Array Collection
> >
> >
> > The thing is my xml is retrieved from a mysql database table which
> > sometimes will have no records or 1 record which will mean the xml will
> > have only 1 or no nodes which in turn causes the problem whereas sometimes
> > it has many records therefore the xml file would have many nodes which
> > would mean no problems.
> >
> > I've heard of looping but can't figure out how to do it or apply it to my
> > code. I've tried simply changing the array collection to xmllist collection
> > but then the data doesn't seem to appear in my tilelist.
> >
> > How would you loop using my specific code there because I always need it
> > returned as an array collection to populate the tilelist but have no idea
> > how to loop?
> >
> > Cheers for your help so far.
> >
> > --- In flexcod...@yahoogro ups.com, Robert Moss <rdm0004@ > wrote:
> > >
> > > We had the same issue, and in our case it was an easy fix. When we read
> > > in the XML it was returning as an ArrayCollection as long as there were
> > > multiple nodes. As you have found, if it was only one node it returned as
> > > a generic object not an ArrayCollection. We were already looping through
> > > the ArrayCollection to populate value objects that we were using to
> > > populate our list. So before the loop we added, if xmlObject is an
> > > ArrayCollection than loop else populate VO with returned data. Worked
> > > without issues ever since.
> > >
> > > Robert
> > >
> > >
> > >
> > >
> > >
> > > ____________ _________ _________ __
> > > From: thomas parquier <mailinglists@ ...>
> > > To: flexcod...@yahoogro ups.com
> > > Sent: Mon, April 19, 2010 9:27:13 AM
> > > Subject: Re: [flexcoders] Problems With my XML Created Array Collection
> > >
> > >
> > > what about xmllistcollection ?;
> > >
> > > ---
> > > thomas parquier
> > > http://www.web- attitude. fr/realisations/
> > > msn : thomas.parquier@ web-attitude. fr
> > > softphone : sip:webattitude@ ekiga.net
> > > tÃÆ'©lÃÆ'©phone portable : +33601 822 056
> > >
> > >
> > >
> > > 2010/4/19 James <garymoorcroft_ i...@yahoo. co.uk>
> > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > >
> > > > >
> > > >
> > > >>
> > > >
> > > >>
> > > >
> > > >In my app I have an array collection which populates a tilelist. This
> > > >array collection is populated by a remote xml file via a http request.
> > > >Problem is if the xml only has 1 or no nodes I get a null object
> > > >reference error. I know this is a known problem but I've never been able
> > > >to find a valid solution for it which could be applied to my code. I've
> > > >heard of looping through the xml to create an array collection or using
> > > >xmllist collection but I don't know how these can be done whilst still
> > > >allowing the data to be displayed in the tilelist. Can anyone help me
> > > >out please?
> > > >
> > > >>The code for my httpservice, array collcection I'm trying to make and
> > > >>the result of the http service and the tilelist it populates is shown
> > > >>below. As I say this all works fine as long as the xml has more than 1
> > > >>node but causes the error if it doesn't:-
> > > >
> > > >>http://www.coolvisi ontest.com/ getlinks. php"/>
> > > >
> > > >><mx:Script>
> > > >> <![CDATA[
> > > >
> > > >> import mx.rpc.events. ResultEvent;
> > > >> import mx.collections. ArrayCollection;
> > > >
> > > >> [Bindable] private var LinksFullAC: ArrayCollection;
> > > >
> > > >> private function linksResultHandler( event:ResultEven t):void
> > > >> {
> > > >> LinksFullAC= LinksService. lastResult. categories. category as
> > > >> ArrayCollection;
> > > >> }
> > > >
> > > >
> > > >> ]]>
> > > >></mx:Script>
> > > >
> > > >><mx:TileList id="linkChoice" dataProvider= "{LinksFullAC} "
> > > >>height="365" width="665"/ >
> > > >
> > > >>Here's my xml structure:-
> > > >
> > > >><categories>
> > > >> <category>
> > > >> <id></id>
> > > >> <label></label>
> > > >> <icon></icon>
> > > >> </category>
> > > >></categories>
> > > >
> > > >>Obviously I need the array collection to be populated by id, label and
> > > >>icon.
> > > >
> > > >>Thanks for any suggestions.
> > > >
> > > >
> > >
> >
>