--- In flexcoders@yahoogroups.com, "djohnson29" <[EMAIL PROTECTED]> 
wrote:
>
> I have noticed some weird behaviour when working with XML vars and
> XMLListCollections.
> 
> I have an xml variable that I am looping through using a for each
> loop.  I want to separate the xml into 2 seperate lists based on a
> value so as I loop through, I am adding each xml node to 2 different
> XmlListCollections.
> 
> Here is my code:
> 
> // myXML is an xml var declared elsewhere in the program
> var xlc1:XMLListCollection = new XMLListCollection();
> var xlc2:XMLListCollection = new XMLListCollection();
>               
> for each(var x:XML in myXML.MyNode){
> 
>     if(x.NodeStatus == 0) 
>         xlc1.addItem(x);
>    else if(x.NodeStatus == 1)
>         xlc2.addItem(x);
> 
> The problem is that the original xml var is getting modified.
> 
> Say for example that my original myXML variable contains 10 nodes, 
and
> there are 7 nodes that have a NodeStatus of 0 and 3 nodes that have 
a
> NodeStatus of 1.  My 2 XMLListCollection vars contain 7, and 3 nodes
> respectively as would be expected.
> 
> However, the original myXML var now contains 9 extra nodes!  19 
nodes
> in total.  Flex somehow is adding all these nodes back onto the
> original xml var except for 1 node from each of the categories.  So 
in
> other words, the original XML var contains all the newly added nodes
> minus 2.
> 
> Is this a bug in Flex? Shouldn't the XMLListCollection just contain
> pointers back to the original XML var?

Someone correct me if I am wrong on this, but I believe that if you 
add XML to an XMLListCollection, you're actually setting its source 
to the XML object.  So essentially both XMLListCollections are 
pointing back to the same source and operating on it.  I'm not sure 
why not all of the nodes are getting added.

Try something like this:

private var xlc1:XMLListCollection=new XMLListCollection
(myXML.myNode);
private var xlc2:XMLListCollection=new XMLListCollection
(myXML.myNode);

private function filterForStatus0(item:XML):Boolean{
return item.NodeStatus==0;
}

private function filterForStatus1(item:XML):Boolean{
return item.NodeStatus==1;
}

public function init(){
xlc1.filterFunction = filterForStatus0;
xlc1.refresh();
xlc2.filterFunction = filterForStatus1;
xlc2.refresh();
}

You should now see the XMLListCollections properly populated.

HTH;

Amy


Reply via email to