AS3: access variable when writing XML code directly in AS3

匿名 (未验证) 提交于 2019-12-03 09:06:55

问题:

how can I access the value of a variable when writing XML code in AS3? something like this:

var myVar:Number = 3;  var xml:XML =     <myXML>         <valueOfMyVar>???</valueOfMyVar>     </myXML> 

what do I have to replace ??? with?

回答1:

ActionScript 3.0 now treats XML as a native data type meaning its no longer parsed as a String. That does bring with it that the old methods of inserting variable values (e.g. “”+myValue+””) no longer apply.

Just take a look at the following snippet of code:

var myVar:Number = 3;  var xml:XML = <myXML>     <valueOfMyVar>{myVar}</valueOfMyVar> </myXML> 

That’s right, the curly braces notation from MXML. One difference though, this is not an active reference to the variable. If you change the value of the variable this will not update your XML (no, not even in Flex ― this is pure AS3 code but you can of course define the XML structure in MXML and take advantage of its data binding features).

Also worth noting is that you don’t put quotes around the curly braces when you use it for an XML attribute, if you do that it’ll treat it as a String rather than eval’ing it. The XML object takes care of generating valid XML from it.



回答2:

var myVar:Number = 3;  var xml:XML =     <myXML>         <valueOfMyVar></valueOfMyVar>     </myXML>  xml.valueOfMyVar[0] = myVar; 


标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!