[英]XML As XMLNode in Actionscript/Flex
How do I get the root node of an XML object in Actionscript? 如何在Actionscript中获取XML对象的根节点?
One would think that I could say this: 有人会认为我可以这样说:
var Node:XMLNode = XMLVar as XMLNode;
But although XMLVar is of type XML, Node will = null. 但是虽然XMLVar是XML类型,但Node = null。
So how do I do it? 那我该怎么做?
You code is a little confusing because you are using uppercase first characters for you variable names. 您的代码有点令人困惑,因为您为变量名使用大写的第一个字符。 Take a look at Adobe's coding conventions for AS3 . 看看Adobe的AS3编码约定 。
var xmlData:XML = new XML();
var node:XMLNode = xmlData.firstChild();
This example is pretty useless, but it just demonstrate the method. 这个例子很没用,但它只是演示了这个方法。
Use the XML object's firstChild method . 使用XML对象的firstChild方法 。 To get the first node as a XMLNode object. 将第一个节点作为XMLNode对象。
var node:XMLNode = xmlVar as XMLNode;
although xmlVar is of type
XML
虽然xmlVar是XML
类型
XML
and XMLNode
are not compatible in ActionScript-3. XML
和XMLNode
在ActionScript-3中不兼容。 XML
class of AS2 (and its related classes) has been renamed to flash.xml.XMLDocument
in AS3. AS2的XML
类(及其相关类)已在AS3中重命名为flash.xml.XMLDocument
。
The
XMLNode
/XMLDocument
class represents the legacyXML
object that was present in ActionScript 2.0 and that was renamed in ActionScript 3.0.XMLNode
/XMLDocument
类表示ActionScript 2.0中存在且在ActionScript 3.0中重命名的旧XML
对象。 In ActionScript 3.0, consider using the new top-levelXML
class and related classes instead, which support E4X (ECMAScript for XML). 在ActionScript 3.0中,请考虑使用新的顶级XML
类和相关类,它们支持E4X(ECMAScript for XML)。 TheXMLNode
/XMLDocument
class is present for backward compatibility.XMLNode
/XMLDocument
类用于向后兼容。
An object of type XML
cannot be cast directly to XMLNode
. XML
类型的对象不能直接转换为XMLNode
。 Since you're trying to cast with as
keyword, it just returns null
instead of raising an alarm saying the cast failed. 由于您尝试使用as
关键字进行强制转换,因此它只返回null
而不是发出警告说投射失败。
And as already mentioned, consider using lowerCase names for variables, as UpperCase names are normally used for naming classes. 如前所述,请考虑对变量使用lowerCase名称,因为UpperCase名称通常用于命名类。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.