views:

78

answers:

1

Here are my functions for conversion:

private function arrCol2XML(arrCol:ArrayCollection):XML
{
 var xml:XML=new XML(<root></root>);
 for (var i:Number=0; i<arrCol.length; i++)
 {
  var obj:Object=arrCol.getItemAt(i);
  xml.appendChild(recursive(obj));
 }
 return xml;
}

private function recursive(obj:Object, str:String='item'):XML
{
 var xml:XML=new XML('<' + str + '></' + str + '>' );
 if(obj is Array)
 {
  for (var tmpObj:Object in obj)
  {
   Alert.show(flash.utils.getQualifiedClassName(tmpObj) + " - " + str);
//   xml.appendChild(recursive(tmpObj as Object, 'item'));
  }
 } else {
  for (var property:String in obj)
  {
   if(obj[property] is Array)
   {
    xml.appendChild(recursive(obj[property] as Array, property));
   } else {
    xml.appendChild(XML("<" + property + ">" + obj[property].toString() + "</" + property + ">"));
   }
  }
 }
 return xml;
}

Here is how I call them:

Alert.show(arrCol2XML(acRoute).toXMLString());

End here is my ArrayCollection:

objBlocat = {title:"Blocate", type:"nivel_blocat", children:[
{ title:"alege departament", type:"alege_departament", raspuns:'aaaa'}
]};
acRoute = new ArrayCollection([objIesire, objBlocat]);

And my output at current state of functions is:

<root>
  <item>
    <type>nivel_blocat</type>
    <children/>
    <mx_internal_uid>32F045BF-24B8-8AA8-3E8D-8F9BF92A0AFC</mx_internal_uid>
    <title>Blocate</title>
  </item>
</root>

And the alert:

int - children

Question: How can I solve this and is there a bug in the API?

PS: I admit the code isn't straight forward because it simply didn't work. PPS: SimpleXMLEncoder throws stack overflow if I have objects that have a property value referencing to other objects... so that's why I want to write my own method for the conversion Any help would be very appreciated! Many thanks!

A: 

solved it myself by using this as recursion():

private function recursive(obj:Object, str:String='item'):XML
{
    var xml:XML=new XML('<' + str + '></' + str + '>' );
    if(obj is Array && obj.length!=0)
    {
        var ac:ArrayCollection=new ArrayCollection(obj as Array);
        var xml2:XML=new XML(<item></item>);
        for (var i:Number=0; i<ac.length; i++)
        {
            var myObj:Object=ac.getItemAt(i);
            for (var prop:String in myObj)
            {
                xml2.appendChild(new XML("<" + prop + ">" + myObj[prop]  + "</" + prop + ">"));
            }
        }
        xml.appendChild(xml2);
    } else {
        for (var property:String in obj)
        {
            if(obj[property] is Array)
            {
                xml.appendChild(recursive(obj[property] as Array, property));
            } else {
                xml.appendChild(XML("<" + property + ">" + obj[property].toString() + "</" + property + ">"));
            }
        }
    }
    return xml;
}
Srdjan