如何在 E4X(特别是 Flex 3)中检测变量父元素的子元素?
我的 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 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
试试这个
它将搜索所有出现的情况
Try this
It will search for all occurrence
我相信您想使用
xml.*.hasOwnProperty('opt_out_flag')
而不是您当前使用的。I believe you want to use
xml.*.hasOwnProperty('opt_out_flag')
rather than what you're currently using.您能否保证
始终位于
元素的第一个子元素中? 如果是这样,类似以下的内容应该有效:(免责声明:我知道这适用于属性,但我不知道它是否适用于子元素)
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)
您可以使用后代方法(沿树递归)来查找标签吗?
希望这就是您正在寻找的。
can you use the descendants method (which recurses down the tree) to find the tag?
Hope this is what you are looking for.
您可以使用
您可以省略 != undefined 部分,但为了便于阅读,我会将其保留在那里。
You can use
you can omit the != undefined part, but I would leave it there for readability.