简体   繁体   中英

Flex-ActionScript :- XML Parsing

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 (ie Buttons, Label) according to their respective properties.

How to do that ?

Thanks.

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;
}

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/

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM