开发者

XML As XMLNode in Actionscript/Flex

开发者 https://www.devze.com 2023-01-01 08:50 出处:网络
How do开发者_StackOverflow中文版 I get the root node of an XML object in Actionscript? One would think that I could say this:

How do开发者_StackOverflow中文版 I get the root node of an XML object in Actionscript?

One would think that I could say this:

var Node:XMLNode = XMLVar as XMLNode;

But although XMLVar is of type XML, Node will = null.

So how do I do it?


var node:XMLNode = xmlVar as XMLNode;

although xmlVar is of type XML

XML and XMLNode are not compatible in ActionScript-3. XML class of AS2 (and its related classes) has been renamed to flash.xml.XMLDocument in AS3.

The XMLNode/XMLDocument class represents the legacy XML object that was present in ActionScript 2.0 and that was renamed in ActionScript 3.0. In ActionScript 3.0, consider using the new top-level XML class and related classes instead, which support E4X (ECMAScript for XML). The XMLNode/XMLDocument class is present for backward compatibility.

An object of type XML cannot be cast directly to XMLNode. Since you're trying to cast with as keyword, it just returns null instead of raising an alarm saying the cast failed.


And as already mentioned, consider using lowerCase names for variables, as UpperCase names are normally used for naming classes.


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.

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. To get the first node as a XMLNode object.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号