【问题标题】:Flex-ActionScript :- XML ParsingFlex-ActionScript :- XML 解析
【发布时间】:2011-11-26 13:58:06
【问题描述】:

伙计们,

我在 ActionScript 中有以下 xml

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

现在,我想读取/解析这个 xml 字符串,然后根据它们各自的属性生成 Flex 控件(即按钮、标签)。

如何做到这一点?

谢谢。

【问题讨论】:

    标签: xml arrays flash actionscript-3 apache-flex


    【解决方案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;
    }
    

    我使用此代码将 xml 转换为对象。那么使用xml真的很简单。

    例如,您的 xml 如下所示:

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

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

    【讨论】:

    • 我们可以将它以名称-值对的形式存储在某个数组集合/字典中吗?我做不到……
    • 嗯,它很好地转换了 xml。然后你有 o.Top.Component.getItemAt(index) 将是一个对象(基本上是一个名称-值对)
    • 你真是个天才! Flex-guru :) .. 你的解决方案工作顺利。来自我的 +1 并接受这个答案。再次感谢:)
    【解决方案2】:

    使用带有 itemRendererFunction 的 DataGroup,它会根据 XML 的属性返回 ClassFactory。你不需要有一个单独的步骤来先把它变成对象。相反,只需执行以下操作:

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

    有关使用自定义 itemRendererFunction 的更多信息,请查看 http://help.adobe.com/en_US/flex/using/WS77c1dbb1bd80d3836ecbb5ec129ec77b1e1-8000.html#WS94F31173-40D5-4ddd-B7B3-17D02BD57EAF

    有关通过 e4x 访问 XML 属性的信息,请参阅 http://dispatchevent.org/roger/as3-e4x-rundown/

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-18
      • 1970-01-01
      相关资源
      最近更新 更多