简体   繁体   English

XML作为Actionscript / Flex中的XMLNode

[英]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. XMLXMLNode在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 legacy XML 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-level XML class and related classes instead, which support E4X (ECMAScript for XML). 在ActionScript 3.0中,请考虑使用新的顶级XML类和相关类,它们支持E4X(ECMAScript for XML)。 The XMLNode / 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.

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