使用XML点符号字符串的Flex

使用XML点符号字符串的Flex,xml,apache-flex,notation,Xml,Apache Flex,Notation,在下面的示例中,第一个跟踪提供节点处的xml数据,但第二个跟踪没有。这是AS3。如何使用变量实现与内联点表示法相同的功能 var x:String = "animXML.home.version"; trace(animXML.home.version); // this works trace([x]); // this does not 谢谢我不确定您想要实现什么,但这将输出相同的结果: var x:String = animXML.home.vers

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

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

谢谢

我不确定您想要实现什么,但这将输出相同的结果:

var x:String = animXML.home.version as String;
trace(animXML.home.version);  // this works
trace(x);                     // this works
更新(完整脚本):


您好,Eduardo,感谢您的建议,AS3中不支持eval。根据您的定义,x是字符串“animXML.home.version”。另外,您所说的“如何使用变量来实现与内联点表示法相同的功能”是什么意思?您好,在第一个跟踪中,我将获得实际的xml数据,在本例中,这些数据将是“版本1”输出到控制台。在第二个跟踪中,我看到输出到控制台的字符串“animXML.home.version”。实际上,这不起作用,因为该字符串是节点的XML点符号,所以第一个跟踪实际上指向XML并输出节点数据,上面的第二个跟踪将只输出x字符串。我用完整的代码更新了答案,以便您可以看到它的工作原理,如果这不是您想要的,请解释。希望能有帮助。干得好,但我的情况不同。对不起。第二个跟踪在上面起作用,因为xml在mxml中。在我的例子中,XML是从外部XML文件读入的,并且有一个可绑定的变量分配给它。当我创建我的问题时,我试图使我的问题尽可能简单,但我没有想到有人会像你那样来解决它,同样做得很好,但我仍然必须找到一种方法,让带有点符号的变量在运行时而不是编译时进行计算。谢谢。当您单击按钮时,它正在运行时评估XML。没有区别:XML就是XML,它与您从web服务获得的XML没有区别。如果你认为你能更好地描述你目前的问题,请打开另一个问题的细节,我会尽力帮助你。干杯
<?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>
Version 1
Version 1