Modern addiction: ActionScript 3.0 Chit chat about AS 3.0, OO concepts, and Flash in general…

18Aug/090

Basic XML checking…

Lots of unexpected errors can come from mistyping XML. So you have wrong data, and you load it to your program and of course it will work wrong.

You can be sure to check your loaded data before doing anything. Also be sure to cast your data when assigning it to variable. In example:

//Sample XML
var xml:XML = <node attribute="1">Node text</node>;
 
//Assign node text to txt variable
var txt:String = null;
if ( "node" in xml ) {
	txt = String( xml.node );
} else {
	trace( "Node doesn't exist!" );
}
 
//Assign node attribute to num variable
var num:uint = 0;
if ( "@attribute" in xml ) {
	num = uint( xml.@attribute );
} else {
	trace( "Attribute doesn't exist!" );
}
Comments (0) Trackbacks (0)

No comments yet.


Leave a comment


No trackbacks yet.