0

Folks,

I have the following xml in ActionScript.

var xml:XML = <Top>
                <Component>
                   <type>Button</type>
                   <id></id>
                   <width>50</width>
                   <height>20</height>
                   <x>0</x>
                   <y>0</y>
                </Component>
                <Component>
                   <type>Label</type>
                   <id></id>
                   <width>30</width>
                   <height>10</height>
                   <x>0</x>
                   <y>0</y>
                </Component>
             </Top>;

Now, I want to read/parse this xml string and then generate Flex controls (i.e Buttons, Label) according to their respective properties.

How to do that ?

Thanks.

2 Answers 2

1
import flash.xml.XMLDocument;
import mx.rpc.xml.SimpleXMLDecoder;
public static function xmlToObject(x:XML):Object{
    var xmlStr:String = x.toString();
    var xmlDoc:XMLDocument = new XMLDocument(xmlStr);
    xmlDoc.ignoreWhite=true;
    var decoder:SimpleXMLDecoder = new SimpleXMLDecoder(true);
    var resultObj:Object = decoder.decodeXML(xmlDoc);
     return resultObj;
}

I use this code to convert xml to Objects. Then it is REALLY simple to use the xml.

For example, your xml would look like:

var xml:XML = <Top>
                <Component>
                   <type>Button</type>
                   <id></id>
                   <width>50</width>
                   <height>20</height>
                   <x>0</x>
                   <y>0</y>
                </Component>
                <Component>
                   <type>Label</type>
                   <id></id>
                   <width>30</width>
                   <height>10</height>
                   <x>0</x>
                   <y>0</y>
                </Component>
             </Top>;

and

var o:Object=xmlToObject(xml);

var top:Object=o.Top;
var componentArrayC:ArrayCollection=top.Component;
for each(var cmp:Object in componentArrayC) {
    //You would have these properties:
    cmp.type;
    cmp.id;
    cmp.width;
    cmp.height;
    cmp.x;
    cmp.y;
}
Sign up to request clarification or add additional context in comments.

3 Comments

Can we do it storing in form of name-value pair in some arraycollection/dictionary ? I am not able to do that ...
Ah well it converts the xml pretty nicely. Then you have o.Top.Component.getItemAt(index) will be an Object (which is a name-value pair basically)
You are genius man! Flex-guru :) .. your soln worked smoothly. +1 from me and accepting this answer. Thanks once again :)
0

Use a DataGroup with an itemRendererFunction that returns a ClassFactory based on the properties of your XML. You don't need to have a separate step to make it into Objects first. Instead, just do something like this:

//yourXML is already populated with your XML
var dataSource:XMLListCollection = new XMLListCollection(yourXML.elements);
//yourDataGroup is defined elsewhere
yourDataGroup.dataProvider = dataSource;

For more on using a custom itemRendererFunction, check out http://help.adobe.com/en_US/flex/using/WS77c1dbb1bd80d3836ecbb5ec129ec77b1e1-8000.html#WS94F31173-40D5-4ddd-B7B3-17D02BD57EAF

For information on accessing the properties of the XML through e4x, see http://dispatchevent.org/roger/as3-e4x-rundown/

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.