Hi, Could someone run this code and let me know whether it is expected behaviour?
The snippet of code (below) is looking for xml nodes which match a certain criteria - it then pops them in an XMLListCollection. I have no problem finding the nodes and populating the collection. However, on the second time around I noticed that my original XML data gets modified as well. I figured that this must be a reference problem, and it does appear to be (see comments in code for solution), but I wouldn't expect to need to do this here, as I'm only reading the data, not writing to it or anything that references it. Below is a the code for the mxml file: 1) Run it in debug mode 2) Choose "Brazil" from the ComboBox 3) Step through it and watch the original xml data (myTestData) change when the second match is added to the XMLListCollection. Any understanding of this would be appreciated ******* Here is the code: ******** <?xml version="1.0" encoding="utf-8"?> <mx:Application xmlns:mx="http://www.adobe.com/2006/mxml" layout="absolute" verticalScrollPolicy="off" horizontalScrollPolicy="off" paddingBottom="5" paddingLeft="5" paddingRight="5" paddingTop="5" height="399" width="514"> <mx:Script> <![CDATA[ import mx.collections.XMLListCollection; import mx.controls.ComboBox; import mx.collections.ArrayCollection; import mx.events.ListEvent; import mx.events.TreeEvent; [Bindable] private var countries:Array = ["all","Brazil","Colombia", "Peru"]; [Bindable] private var description:String = ""; private var myTestData:XML = <tree> <node> <label>AAA</label> <country>Kenya</country> </node> <node> <label>BBB</label> <country>Brazil</country> </node> <node> <label>CCC</label> <country>Brazil</country> </node> <node> <label>DDD</label> <country>Peru</country> </node> </tree>; private function onCountryComboChange(event:ListEvent):void { var countryString : String = ComboBox(event.currentTarget ).selectedItem.toString(); var nodesMatchingQuery : XMLListCollection = new XMLListCollection(); var CHECKMYORIGDATA = myTestData; if(countryString != "all") { //SOLUTION IS TO LOOP THROUGH myTestData..node.copy() - BUT //WHY SHOULD I NEED TO LOOP THROUGH A COPY WHEN I'M ONLY READING THE DATA? for each(var theNode:XML in myTestData..node) { var keywords:XMLList = theNode.country; var numMatches:uint = keywords.(text()==countryString).length(); if(numMatches >= 1){ //STEP THROUGH THIS CODE, LOOKING AT THE ORIGINAL XML nodesMatchingQuery.addItem(theNode); //THIS ALTERS myTestData ON SECOND ATTEMPT } } } } ]]> </mx:Script> <mx:VBox height="100%"> <mx:ComboBox width="141" dataProvider="{countries}" change="onCountryComboChange(event)"/> </mx:VBox> </mx:Application>