as3 xml check if element exists

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.

<items>
    <product>
        <resourceImages>
            <image />
        </resourceImages>
    </product>
    <product>
        <resourceImages>
            <image />
        </resourceImages>
    </product>
    <prepack>
        <resourceImages />
    </prepack>
    <product>
        <resourceImages>
            <image />
        </resourceImages>
    </product>
    <prepack>
        <resourceImages />
    </prepack>
</items>

Answers


Like this?

for each(var item : XML in xmlData.children())
{
    var hasImages : Boolean = (item.resourceImages.children().length() > 0);

    if(hasImages)
        trace("item has images")
}

It appears there are two ways, but the check using undefined seems preferable.

if (item.image.length() > 0)

OR

if (item.image != undefined)

But beware, this always evaluates to true regardless if the node exists.

if (item.image)

Which is weird considering the undefined check.


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
    }
}

Need Your Help

Stop Solver after it has reached a certain number of dps

excel vba excel-vba

Is there any way to stop solver after it has found a solution to 2 dps and move on to the next solve in the loop?

In C, whether the behavior of adding a number i to a pointer adds i*sizeof(datatype) is compiler dependent?

c arrays pointers

In the following pseudo code, when a number i is added to the pointer abc, the next array index is pointed.

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.