One more suggestion, regarding attributes.

 

If you use an expression with the "@" symbol and the attribute does not
exist on any of the nodes processed, you will get an error.  Using the
"attribute()" method avoids this by returning an empty XMList.

 

For example:

<root>

 <node id="xxx" />

 <node cat="yyy" / >

</root>

 

var xlCat:XMLList = root.node(@cat=="yyy"); // will error

var xlCat:XMLList = root.node(attribute("cat")=="yyy"); // will NOT
error

 

Tracy

 

________________________________

From: flexcoders@yahoogroups.com [mailto:[EMAIL PROTECTED] On
Behalf Of Paul deCoursey
Sent: Sunday, October 07, 2007 8:55 PM
To: flexcoders@yahoogroups.com
Subject: Re: [flexcoders] Detecting optional elements in XML

 

Use e4x, most e4x expressions return an XMLList. Then check if the 
result has a length greater than 0. Some of the alternatives are 
actually more work computationally, as they do the test for existence 
and then run the query later if it exists. Here is an example of where 
I use it.

var previewsElement:XMLList = xml.previews;
if(previewsElement.length()>0) {
//do stuff here
}

In some cases I just get the list and iterate over it, then if the list 
is empty it just moves on.

Paul

John M. Resler wrote:
> Hello all,
> I'm pretty new to this dynamic web content thing and I'm wondering 
> how to deal with the AS3 XML set of objects. In particular, how do you

> program to deal with an element that may not be there at runtime? In 
> specific, RSS has no official schema and so some people don't include 
> all of the elements. If I reference an XML element that does not exist

> I get an exception. Is there a way to detect the existence of an 
> element? Thanks for your help.
>
> - J.R.
> 
> A people that values their privileges
> over their principles, will soon lose
> both.
> 
> - Dwight "Ike" Eisenhower
>
> 

 

Reply via email to