Connecting Tech Pros Worldwide Forums | Help | Site Map

XML to ArrayCollection, sort and then convert to XML again

Newbie
 
Join Date: May 2008
Posts: 16
#1: Jan 8 '09
Hi everyone,

Please tell me what I am doing wrong:

I have an xml data coming to my Air application that looks something like this:
Expand|Select|Wrap|Line Numbers
  1. <project name="Sales Demos" path="/Company Home/Client Spaces/Sales Demos" type="{http://www.myCo.org/model/content/1.0}folder" id="71f36dcd-b59b-11dd-8205-5992b4466fe5" modified="Nov 18, 2008 3:51:20 PM" size="" icon32="/images/icons/default.gif" viewurl=""/>
  2. <project name="Something else" ...
  3. etc...
  4. />
  5.  
I want to place the XML into an ArrayCollection, so that I can sort it by the values referenced in the "name" attribute.

Expand|Select|Wrap|Line Numbers
  1.             import mx.collections.Sort;
  2.             import mx.collections.SortField;
  3.             import mx.collections.ArrayCollection;
  4.  
  5.                 var arrColl:ArrayCollection = new ArrayCollection;
  6.                 for each(var s:XML in tree.data.children())
  7.                 {
  8.                       arrColl.addItem(s);
  9.                 }
  10.  
  11.  
  12.                 /* Create the SortField object for the "data" field in the ArrayCollection object, and make sure we do a numeric sort. */
  13.                 var dataSortField:SortField = new SortField();
  14.                 dataSortField.name = "name";
  15.                 dataSortField.caseInsensitive = true;
  16.  
  17.                 /* Create the Sort object and add the SortField object created earlier to the array of fields to sort on. */
  18.                 var numericDataSort:Sort = new Sort();
  19.                 numericDataSort.fields = [dataSortField];
  20.  
  21.                 /* Set the ArrayCollection object's sort property to our custom sort, and refresh the ArrayCollection. */
  22.                 arrColl.sort = numericDataSort;
  23.                 arrColl.refresh();
  24.                 var someArrColToString:String = "<xml>";
  25.                 for each(var XMLNodeObj:Object in arrColl)
  26.                 {
  27.                      someArrColToString += "<project name='" + XMLNodeObj.name +
  28.                       "' path='" + XMLNodeObj.path + "' type='" +XMLNodeObj.type + 
  29.                       "' id='" + XMLNodeObj.id +"' modified='" +XMLNodeObj.modified +
  30.                       "' size='" + XMLNodeObj.size +"' icon32='" +XMLNodeObj.icon32 +
  31.                       "' viewurl='" + XMLNodeObj.viewurl +
  32.                       "' />";
  33.                     }
  34.                 someArrColToString += "</xml>";
  35.  
  36.                 var someStringToXml:XML = new XML(someArrColToString);
  37.                 trace("tree.data.children() "+tree.data.children()+" someArrColToString "+someArrColToString);
  38.  
I do a trace at the end to see what I've created and the tree.data.children() has the original xml in it's original order, but this is what I get back with the trace of var someArrColToString:

Expand|Select|Wrap|Line Numbers
  1. <xml><project name='' path='' type='' id='' modified='' size='' icon32='' viewurl='' /><project name='' path='' type='' id='' modified='' size='' icon32='' viewurl='' /><project name='' path='' type='' id='' modified='' size='' icon32='' viewurl='' /><project name='' path='' type='' id='' modified='' size='' icon32='' viewurl='' /><project name='' path='' type='' id='' modified='' size='' icon32='' viewurl='' /><project name='' path='' type='' id='' modified='' size='' icon32='' viewurl='' /><project name='' path='' type='' id='' modified='' size='' icon32='' viewurl='' /><project name='' path='' type='' id='' modified='' size='' icon32='' viewurl='' /><project name='' path='' type='' id='' modified='' size='' icon32='' viewurl='' /><project name='' path='' type='' id='' modified='' size='' icon32='' viewurl='' /><project name='' path='' type='' id='' modified='' size='' icon32='' viewurl='' /><project name='' path='' type='' id='' modified='' size='' icon32='' viewurl='' /><project name='' path='' type='' id='' modified='' size='' icon32='' viewurl='' /></xml>
  2.  
Which is showing me the correct amount of project tags but the attributes are not being filled.

Do I have to do something like XMLNodeObj.attribute.name?

What am I doing wrong?

Thanx n advance,
me
Reply