I am loading a master XML file and then trying to break it down into sections and I’m looking for some help. I’ve had some help already with this, but I’m stuck at the last part.
Here’s my XML file:
<listarray>
<items>
<entry type="ecard" num="2">
<title>Ecard 2</title>
</entry>
<entry type="ecard" num="1">
<title>Ecard 1</title>
</entry>
<entry type="ecard" num="3">
<title>Ecard 3</title>
</entry>
<entry type="website" num="1">
<title>Website 1</title>
</entry>
<entry type="website" num="2">
<title>Website 2</title>
</entry>
</items>
</listarray>
Here’s the code I’m using to load the XML:
import mx.xpath.XPathAPI;
portfolioXML = new XML();
portfolioXML.ignoreWhite = true;
processXML = function(XMLfilename){
portfolioXML.load(XMLfilename);
portfolioXML.onLoad = function() {
imgsArray = this.firstChild.firstChild.childNodes;
totalIMGs = imgsArray.length;
ecardArray = XPathAPI.selectNodeList(this.firstChild,"/listarray/items/entry[@type='ecard']");
}
}
processXML("xmlfile.xml");
So I was able to create a new array that contains only entries with the attribute “ecard” in them, but now I want to re-order that array into numerical order based on the attribute “num” so that it lists as Ecard 1, Ecard 2, Ecard 3 in the XML. How would I go about re-ordering the new array I’ve created?
You should parse the xml inside one unique Array, and treat each position as an object with the attributes. Then you can use sortOn function to reorder the Array using one of the props inside it.
http://livedocs.adobe.com/flash/9.0/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00001364.html
[QUOTE=chiqui;2323831]You should parse the xml inside one unique Array, and treat each position as an object with the attributes. Then you can use sortOn function to reorder the Array using one of the props inside it.
http://livedocs.adobe.com/flash/9.0/main/wwhelp/wwhimpl/common/html/wwhelp.htm?context=LiveDocs_Parts&file=00001364.html[/QUOTE]
Can you explain this a little more? I’m still new to XML so I don’t have all the functions down yet. Are you saying that I should just re-order the main “imgsArray” array and not the new broken down “ecardArray” array?
I took a look at that link, but it’s talking about removing a value from the array when I just want to re-order it all.
Big sorry u posted the wrong link :S
sortOn orders the array by its properties. Here is a small example (paste in flash to see the output results!):
[AS]var myArray:Array=new Array();
myArray[0]= new Object();
myArray[0].id=0;
myArray[0].price=22;
myArray[0].newColor=“ORANGE”
myArray[1]= new Object();
myArray[1].id=1;
myArray[1].price=12;
myArray[1].newColor=“red”
myArray[2]= new Object();
myArray[2].id=3;
myArray[2].price=16;
myArray[2].newColor=“PINK”
trace(myArray[0].id);
myArray.sortOn(id, Array.DESCENDING); //DESCENDING ORDER BY ID VARIABLE
trace(myArray[0].id);[/AS]
sortOn allows also other types of ordering.
[QUOTE=chiqui;2323846]Big sorry u posted the wrong link :S
sortOn orders the array by its properties. Here is a small example (paste in flash to see the output results!):
[AS]var myArray:Array=new Array();
myArray[0]= new Object();
myArray[0].id=0;
myArray[0].price=22;
myArray[0].newColor=“ORANGE”
myArray[1]= new Object();
myArray[1].id=1;
myArray[1].price=12;
myArray[1].newColor=“red”
myArray[2]= new Object();
myArray[2].id=3;
myArray[2].price=16;
myArray[2].newColor=“PINK”
trace(myArray[0].id);
myArray.sortOn(id, Array.DESCENDING); //DESCENDING ORDER BY ID VARIABLE
trace(myArray[0].id);[/AS]
sortOn allows also other types of ordering.[/QUOTE]
I looked at this in Flash, but it doesn’t appear to be giving the correct result. I see:
0
3
in the output window. Was it supposed to remove id1?
Do you know of a way I could apply sortOn to the array I’ve already created within the XML file? I’m really hoping to not have to go back and change all the AS code I’ve already created. Thanks!
The output trace 0 because the id on the array at position [0] is 0. Once you run sortOn function over the array using Array.DESCENDING the entire order of the aray is inverted, since the largr value is 3, then 1 and then 0 on positions [0], [1], [2].
I cant figure out another way to reorder the xml. Just go trough it using a for sentence and put into an array!
Whit a structure (for example) like this:
ActionScript Code:
[LEFT]entry[COLOR=#000000][[/COLOR][COLOR=#000080]0[/COLOR][COLOR=#000000]][/COLOR].[COLOR=#000080]ecard[/COLOR]
entry[COLOR=#000000][[/COLOR][COLOR=#000080]0[/COLOR][COLOR=#000000]][/COLOR].[COLOR=#000080]title[/COLOR]
entry[COLOR=#000000][[/COLOR][COLOR=#000080]0[/COLOR][COLOR=#000000]][/COLOR].[COLOR=#000080]num[/COLOR]
[/LEFT]
Once you have the properties of each entry inside the array, just use entry.sortOn(ecard); to order the entire array by that single property.
[QUOTE=chiqui;2323860]The output trace 0 because the id on the array at position [0] is 0. Once you run sortOn function over the array using Array.DESCENDING the entire order of the aray is inverted, since the largr value is 3, then 1 and then 0 on positions [0], [1], [2].
I cant figure out another way to reorder the xml. Just go trough it using a for sentence and put into an array!
Whit a structure (for example) like this:
ActionScript Code:
[LEFT]entry[COLOR=#000000][[/COLOR][COLOR=#000080]0[/COLOR][COLOR=#000000]][/COLOR].[COLOR=#000080]ecard[/COLOR]
entry[COLOR=#000000][[/COLOR][COLOR=#000080]0[/COLOR][COLOR=#000000]][/COLOR].[COLOR=#000080]title[/COLOR]
entry[COLOR=#000000][[/COLOR][COLOR=#000080]0[/COLOR][COLOR=#000000]][/COLOR].[COLOR=#000080]num[/COLOR]
[/LEFT]
Once you have the properties of each entry inside the array, just use entry.sortOn(ecard); to order the entire array by that single property.[/QUOTE]
I’m really just not understanding this whole thing that well. Could you help me with a full example of an array using my information that I can test out? I can’t get anything to happen with sortOn at all right now.