简体   繁体   中英

XML As XMLNode in Actionscript/Flex

How do 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?

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM