I want to check if the element in this structure exists for each child. The problem is that the children don't have the same name (product,prepack) and I don't want to change the order. Additionally I can't change the XML structure.
我想检查每个子节点中是否存在此结构中的元素。问题是孩子们没有相同的名字(产品,预包装),我不想改变订单。另外,我不能改变XML结构。
<items>
<product>
<resourceImages>
<image />
</resourceImages>
</product>
<product>
<resourceImages>
<image />
</resourceImages>
</product>
<prepack>
<resourceImages />
</prepack>
<product>
<resourceImages>
<image />
</resourceImages>
</product>
<prepack>
<resourceImages />
</prepack>
</items>
3 个解决方案
#1
7
Like this?
喜欢这个?
for each(var item : XML in xmlData.children())
{
var hasImages : Boolean = (item.resourceImages.children().length() > 0);
if(hasImages)
trace("item has images")
}
#2
10
It appears there are two ways, but the check using undefined seems preferable.
看来有两种方法,但使用undefined的检查似乎更可取。
if (item.image.length() > 0)
OR
要么
if (item.image != undefined)
But beware, this always evaluates to true regardless if the node exists.
但请注意,无论节点是否存在,这始终都会为true。
if (item.image)
Which is weird considering the undefined check.
考虑到未定义的检查,这很奇怪。
#3
6
Depends also how is your first loop, but you can also check if the node is not undefined
:
还取决于你的第一个循环,但你也可以检查节点是否未定义:
var xml:XML=<items>
<product>
<resourceImages>
<image />
</resourceImages>
</product>
<product>
<resourceImages>
<image />
</resourceImages>
</product>
<prepack>
<resourceImages />
</prepack>
<product>
<resourceImages>
<image />
<image />
</resourceImages>
</product>
<prepack>
<resourceImages />
</prepack>
</items>;
//loop on all all resourceImage node
for each (var resourceImageXML:XML in xml..resourceImages){
// and check if node if defined
if (resourceImageXML.image != undefined) {
// ok node have image
}
}
#1
7
Like this?
喜欢这个?
for each(var item : XML in xmlData.children())
{
var hasImages : Boolean = (item.resourceImages.children().length() > 0);
if(hasImages)
trace("item has images")
}
#2
10
It appears there are two ways, but the check using undefined seems preferable.
看来有两种方法,但使用undefined的检查似乎更可取。
if (item.image.length() > 0)
OR
要么
if (item.image != undefined)
But beware, this always evaluates to true regardless if the node exists.
但请注意,无论节点是否存在,这始终都会为true。
if (item.image)
Which is weird considering the undefined check.
考虑到未定义的检查,这很奇怪。
#3
6
Depends also how is your first loop, but you can also check if the node is not undefined
:
还取决于你的第一个循环,但你也可以检查节点是否未定义:
var xml:XML=<items>
<product>
<resourceImages>
<image />
</resourceImages>
</product>
<product>
<resourceImages>
<image />
</resourceImages>
</product>
<prepack>
<resourceImages />
</prepack>
<product>
<resourceImages>
<image />
<image />
</resourceImages>
</product>
<prepack>
<resourceImages />
</prepack>
</items>;
//loop on all all resourceImage node
for each (var resourceImageXML:XML in xml..resourceImages){
// and check if node if defined
if (resourceImageXML.image != undefined) {
// ok node have image
}
}