Posts Tagged ‘ XML ’

Util for Picking Random XML Entry

Code for picking up random XML entries from XMLList : code

XML to Object

The Project i have created converts the XML into required object in simple steps. Have a look on it : XSLT

Convert Data Types: ArrayCollection, XML, Object, String

The code below demonstrates the data type conversions among basic DataTypes used most in Flex. The ‘init()’ method does to and from conversion for ArrayCollection to XML and XML to ArrayCollection

Object, String, XML, ArrayCollection

 

import mx.collections.ArrayCollection;
import mx.rpc.xml.SimpleXMLEncoder;
import mx.rpc.xml.SimpleXMLDecoder;
import mx.utils.ObjectUtil; 
  private function xmlToArrayCollection(xml:XML):ArrayCollection{                 
            var xmlDoc:XMLDocument = new XMLDocument(xml.toString());
           var decoder:SimpleXMLDecoder = new SimpleXMLDecoder(true);
            var resultObj:Object = decoder.decodeXML(xmlDoc);
            var ac:ArrayCollection =new ArrayCollection(Array(resultObj.root.list.source.item));
            return ac;
}
private function objectToXML(obj:Object):XML {
            var qName:QName = new QName(“root”);
            var xmlDocument:XMLDocument = new XMLDocument();
            var simpleXMLEncoder:SimpleXMLEncoder = new SimpleXMLEncoder(xmlDocument);
            var xmlNode:XMLNode = simpleXMLEncoder.encodeValue(obj, qName, xmlDocument);
            var xml:XML = new XML(xmlDocument.toString());
            return xml;
}
private function objectToArrayCollection(obj:Object):ArrayCollection {
            var ac:ArrayCollection =  new ArrayCollection(obj as Array);
            return ac;

private function arrayCollectionToXML(ac:ArrayCollection):XML{
var xml:XML = objectToXML(ac);
return xml;
}
private function init():void{
  var arr:Array = new Array();
  arr.push({data:0,name:’deva’});
  arr.push({data:1,name:’raj’});
  var ac:ArrayCollection = new ArrayCollection(arr);
  var xml:XML = arrayCollectionToXML(ac);
var newAc:ArrayCollection = xmlToArrayCollection(xml);
trace(newAc[0][0].name);
}
private function objToStr(value:Object,indent:int =0,
                                         refs:Dictionary= null, 
                                         namespaceURIs:Array = null, 
                                         exclude:Array = null):String{
var str:String;
var refCount:int = 0;
if (value is Date)
            {
                return value.toString();
            }
            else if (value is XMLNode)
            {
                return value.toString();
            }
            else if (value is Class)
            {
                return “(” + getQualifiedClassName(value) + “)”;
            }
            else
            {
                var classInfo:Object = ObjectUtil.getClassInfo(value, exclude,
                    { includeReadOnly: true, uris: namespaceURIs });
                var properties:Array = classInfo.properties;
                str = “(” + classInfo.name + “)”;
                if (refs == null)
                    refs = new Dictionary(true);
                var id:Object = refs[value];
                if (id != null)
                {
                    str += “#” + int(id);
                    return str;
                }
                if (value != null)
                {
                    str += “#” + refCount.toString();
                    refs[value] = refCount;
                    refCount++;
                }
                var isArray:Boolean = value is Array;
                var isDict:Boolean = value is Dictionary;
                var prop:*;
                indent += 2;
                for (var j:int = 0; j < properties.length; j++)
                {
                    str = newline(str, indent);
                    prop = properties[j];
                    if (isArray)
                        str += “[“;
                    else if (isDict)
                        str += “{“;
                    if (isDict)
                    {
                        str += objToStr(prop, indent, refs,
                                                namespaceURIs, exclude);
                    }
                    else
                    {
                        str += prop.toString();
                    }
                    if (isArray)
                        str += “] “;
                    else if (isDict)
                        str += “} = “;
                    else
                        str += ” = “;
                    try
                    {
                        str += objToStr(value[prop], indent, refs,
                                                namespaceURIs, exclude);
                    }
                    catch(e:Error)
                    {
                        str += “?”;
                    }
                }
                indent -= 2;
                return str;
            }
}
private static function newline(str:String, n:int = 0):String
   {
       var result:String = str;
       result += “\n”;
       for (var i:int = 0; i < n; i++)
       {
           result += ” “;
       }
       return result;
   }
/* General function for Conversion (thanks, Krystian Bień)
 public function xmlToArrayCollection(xml:XML):ArrayCollection{
            var temp:String = ‘<items>’ + xml.toString() + ‘</items>’;
            xml = XML(temp);
            var xmlDoc:XMLDocument = new XMLDocument(xml.toString());
            var decoder:SimpleXMLDecoder = new SimpleXMLDecoder(true);
            var resultObj:Object = decoder.decodeXML(xmlDoc);
            var ac:ArrayCollection;
            ac = new ArrayCollection();
            ac.addItem(resultObj.items);
            return ac;                
        }*/

Elearning using Flex

I am glad to announce the first Step for basic requirements of E-Learning using Flex has been achieved.
Basic Requirements met:

  • All Flash player versions starting from Flash Player version 6 to 10, should be loaded. (For support of old elearning courses).
  • Without any memory leak due to loading multiple swf files.
  • Ability to navigate to any particular lesson (SWF).
  • XML Support for loading swf files into a lesson.
  • PreLoader for showing the loading status of individual SWF files
  • Ability to detect events of SWF files (eg: totalframes, swf playing etc.,) 

See the demo of loading a number of animations. The source files include floader.mxmlForcibleLoader.as and XML

Using IViewCursor for XML Traversing

The source code for using IViewCursor for XMLListCollection Navigation is here.

Data Filter

Check the demo for data filtering xml output with LOGICAL AND, updating the Datagrid dynamically.
The source is available over here. It uses data filter component developed by Jack Herrington.