如何在 E4X(特别是 Flex 3)中检测变量父元素的子元素?

发布于 2024-07-27 22:33:52 字数 571 浏览 8 评论 0原文

我的 XML 如下所示:

<question>
    <type_elt>
        <opt_out_flag />
    </type_elt>
</question>

type_elt 不是元素名称; 它可能是 或其他在运行时确定的内容。 鉴于此,我如何检测 opt_out_flag 元素的存在?

我尝试了这个(其中 xml 指的是 question 元素):

if (xml.*.opt_out_flag) {
    do_something();
}

但即使在没有 opt_out_flag 的情况下,上述表达式也会返回 true. 显然我错过了一些东西,但它是什么?

I have XML that looks like this:

<question>
    <type_elt>
        <opt_out_flag />
    </type_elt>
</question>

type_elt is not an element name; it might be <single>, <multiple> or something else, determined at runtime. How, given this, can I detect the presence of the opt_out_flag element?

I tried this (where xml refers to the question element):

if (xml.*.opt_out_flag) {
    do_something();
}

but even in cases without opt_out_flag the above expression returns true. Obviously I'm missing something, but what is it?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

二智少女猫性小仙女 2024-08-03 22:33:52

试试这个

xml..opt_out_flag

它将搜索所有出现的情况

Try this

xml..opt_out_flag

It will search for all occurrence

奶茶白久 2024-08-03 22:33:52

我相信您想使用 xml.*.hasOwnProperty('opt_out_flag') 而不是您当前使用的。

I believe you want to use xml.*.hasOwnProperty('opt_out_flag') rather than what you're currently using.

白馒头 2024-08-03 22:33:52

您能否保证 始终位于 元素的第一个子元素中? 如果是这样,类似以下的内容应该有效:(

免责声明:我知道这适用于属性,但我不知道它是否适用于子元素)

if( "opt_out_flag" in xml.children()[0] ) {
    doSomething();
}

Can you be guaranteed that <opt_out_flag/> will always be in the first child of the <question> element? If so, something like the following should work:

(disclaimer: I know this works with attributes, but I don't know if it will work with child elements)

if( "opt_out_flag" in xml.children()[0] ) {
    doSomething();
}
花想c 2024-08-03 22:33:52

您可以使用后代方法(沿树递归)来查找标签吗?

var optOutNodes:XMLList = xml.descendants("opt_out_flag");

if(optOutNodes.length())
{
   //do code here
}

希望这就是您正在寻找的。

can you use the descendants method (which recurses down the tree) to find the tag?

var optOutNodes:XMLList = xml.descendants("opt_out_flag");

if(optOutNodes.length())
{
   //do code here
}

Hope this is what you are looking for.

薔薇婲 2024-08-03 22:33:52

您可以使用

var optOut:Boolean = xml..opt_out_flag != undefined

您可以省略 != undefined 部分,但为了便于阅读,我会将其保留在那里。

You can use

var optOut:Boolean = xml..opt_out_flag != undefined

you can omit the != undefined part, but I would leave it there for readability.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文