Flex使用XML点表示法字符串

时间:2012-03-08 18:49:58

标签: xml flex notation

在下面的示例中,第一个跟踪为我提供了节点上的xml数据,但第二个跟踪没有。这是AS3。我如何使用变量与内联点表示法相同?

var x:String = "animXML.home.version";
trace(animXML.home.version);  // this works
trace([x]);                   // this does not

由于

1 个答案:

答案 0 :(得分:1)

不确定你想要实现的目标,但这会产生同样的结果:

var x:String = animXML.home.version as String;
trace(animXML.home.version);  // this works
trace(x);                     // this works

更新(完整脚本):

<?xml version="1.0" encoding="utf-8"?>
<s:Application xmlns:fx="http://ns.adobe.com/mxml/2009" 
    xmlns:s="library://ns.adobe.com/flex/spark" 
    xmlns:mx="library://ns.adobe.com/flex/mx" minWidth="955" minHeight="600">

    <fx:Declarations>
        <fx:Model id="animXML">
            <root>
                <home>
                    <version>Version 1</version>
                </home>
            </root>
        </fx:Model>    
    </fx:Declarations>

    <fx:Script>
        <![CDATA[
            protected function clickHandler(event:MouseEvent):void
            {
                var x:String = animXML.home.version as String;
                trace(animXML.home.version);  // this works
                trace(x);                     // this works
            }

        ]]>
    </fx:Script>

    <s:Button label="test" click="clickHandler(event)" />

</s:Application>

点击&#34;测试&#34;按钮提供以下输出:

Version 1
Version 1