loop through complex xml nodes actionscript - actionscript-3

I am reading an xml file in actionscript.
I need to loop through each node named "entry" as showing in below image :-
Can anyone help me ?
I am trying below code. but it not working :-
var categoryList:XMLList = x.feed.#entry;
for each(var category:XML in categoryList)
{
trace(category.#name);
for each(var item:XML in category.item)
{
trace(" "+item.#name +": "+ item);
}
}
"entry" node also has some inner nodes, I also want to read those nodes.
Thanks

This XML is using the namespace http://www.w3.org/2005/Atom, so you have to account for that:
var n:Namespace = new Namespace("http://www.w3.org/2005/Atom");
var categoryList:XMLList = x.n::entry;
Update:
In order to access child nodes, you will need to continue to use the namespace
for each(var category:XML in categoryList)
{
// this traces the name of the author
trace(category.n::author.n::name.toString());
}

Better is:
var n:Namespace = new Namespace("http://www.w3.org/2005/Atom");
default xml namespace = n;
var categoryList:XMLList = x.entry;//no namespace type access
//etc
default xml namespace = null;

Change declaration of categoryList to:
var categoryList:XMLList = x.entry;
It should loop through entry nodes now.

Related

AS3 Referencing an object or variable via string etc

I have found answers 'similar' to the one I'm looking for. I really hope I didn't overlook an already answered problem.
code:
var Randy:Object = {age:32, gender:1};
var Joey:Object = {age:35, gender:1};
var slot_0 = Randy;
var slot_1 = Joey;
myFunction();
function myFunction():void{
for(var i = 0; i < 2; i++){
var thisObject = ("Slot_" + i);
trace(thisObject); // example 1
trace(thisObject.age); //example 2
}
}
it will trace in //example 1
slot_0
slot_1
*if I 'trace(thisObject)' the 'name of the Objects' ("slot_0" ; "slot_1") trace out.*
but in //example 2 I get:
Error #1069: Property age not found on String and there is no default value.
*How do I get it to understand that I want it to reference the properties of the object itself? e.g. 'trace(thisObject.age) means slot_0.age witch means Randy.age etc...*
Without a for-loop, I have to write a lot of redundant script, so I need to know this!
Thank You in advance for the help!
var thisObject = this["slot_"+i];
That's how you can do a string reference. Of course, if the var is located elsewhere, use the proper parent object.

flash get function's argument name

I want to get all arguments names of a function inside the function
example:
function fct(var1:string,var2:string){
var names:Array=...
trace(names);
}
must trace : var1,var2
Thanks!
Simply put, this is not possible. The closest you can get is the argument number and value. See below:
function fct( ... args ):void {
for ( var v:Object in args ) {
trace( v + ": " + args[v] );
}
}
var str1:String = "this is a test";
var str2:String = "this is another test";
fct( str1, str2 );
//output
//0: this is a test
//1: this is another test
For future reference, you can use ... + a variable name to allow for as many arguments as you need. Regardless, you should just need to access args[ INDEX ] rather than the actual variable name, which you wouldn't be able to access anyway because there would be no way to apply scope (such as variableName[ "propertyName" ])
It is impossible like native method, but you can use metadata tag to set arguments names. I create simple example. But i don't understand how it can help you in real projects:
[Arguments(param1="arg1",param2="arg2")]
public function test(arg1:Number, arg2:Number):void {
var desc_xml:XML = describeType(Object(this).constructor);
var metas_xml:XMLList = desc_xml.factory.method.(#name == "test");
var args_xml:XMLList = metas_xml.metadata.(#name == "Arguments");
for each (var argx:XML in args_xml.arg)
{
trace(argx.#value.toXMLString());
}
};
I use flex 4.6. Don't forget add each existing Metadata tags to the compiler argument with “-keep-as3-metadata+=Arguments”. It need for compile release versions.

How to use a variable as part of an URL

I have a variable
var qstAccessCode:String = "default";
and a loader with URLRequest
var qst:XML;
var qstLoader:URLLoader = new URLLoader();
qstLoader.load(new URLRequest("http://dl.dropbox.com/u/44181313/Qaaps/Audio/" + qstAccessCode + ".qst"));
qstLoader.addEventListener(Event.COMPLETE, processQST);
function processQST(e:Event):void {
qst = new XML(e.target.data);
trace("QST loading");
}
I would like to use the value of qstAccessCode to complete the URL (so I can change the URL based on user input - if no input then use "default") but I get an error:
"1120: Access of undefined property qstAccessCode"
Is this to do with scoping? How can I complete the URL? Thanks in advance.
Edit: I haven't been able to get clear on this, so I'm also going to look at generating the complete URL from the user-input function and see if I get the URLRequest to pick it up as a variable. If there are any further comments on the original idea I will be very grateful to read them. Cheers.
Edit: #Moorthy I have qstAccessCode defined like this:
var qatAccessCode:String = "default";
var stageText:StageText = new StageText();
stageText.returnKeyLabel = ReturnKeyLabel.GO;
stageText.stage = this.stage;
stageText.viewPort = new Rectangle(225, 765, 200, 35 );
stageText.addEventListener(Event.CHANGE, onChange);
function onChange(e:Event):void
{
qatAccessCode = stageText.text;
trace(qatAccessCode);
}
It traces keyboard entry when I test movie (Air 3.2 for Android).
qstAccessCode should be defined in the same scope as the URLRequest.
You must defined property qstAccessCode like:
var qstAccessCode:string;
qstAccessCode's value is your url address.

new constructor with a string

Instead of many if conditionals, I want to call a constructor according to a string value
var valueString:String = "myNewClassB";
var value:Class = valueString as Class;
new value() // new value() == new myNewClassB()
I know it's gonna fail, I need help. Thanks.
var ClassReference:Class = getDefinitionByName("myNewClassB");
var instance = new ClassReference();
That's the basics, bud.
If you want to do that, there are two ways, either assign classes to a list of classes made for an example in a object:
var list:Object = {
classA: FirstClass,
classB: SecondClass,
classC: ThirdClass
}
and than call them by a string:
var desiredObject:* = new (list["classA"] as Class)();
or you could also use getDefinitionBtName but than if you want to get a class you need to provide a full name (with the package)
var desiredClass = getDefinitionByName( "com.somedomain.SomeClass" );
If you are laoding an SWF content and than want to get a class from it you should use that loader loaderInfo.applicationDomain.getDefinition( "....class" );
you can also check if a class is defined by:
loaderInfo.applicationDomain.hasDefinition( "....class" );
link: ApplicationDomain.getDefinition
link: ApplicationDomain.hasDefinition
link: LoaderInfo

Delete node of type Object from flex Tree component?

I have a tree with nodes, and a delete button, first user select the node and click this delete button, I want this node to be removed from the tree, Its not XML, every node in tree is of type Object
{label:'folder',children:[{label:'file1'}]}
I tried delete myTree.selectedItem (but compiler wont let me do it)
also tried myTree.selectedItem = null (just unselects the item)
and also how can I access reference to parent object of myTree.selectedItem?
Without a parent node reference this is going to be quite hard. I would suggest to create a class TreeNode or so instead of a vanilla object. Besides the "label" and the "children" property, give the node a "parent" property and set the parent when you create the model for the tree.
Then when you select and item and click the remove button, you can get the parent node of the selected node and call a "removeChild" or so on it. This should then remove the given childnode.
It might be that you need to invalidate the model of tree after removing a node. You can do this with:
myTree.invalidateList();
var item:* = tree.selectedItem;
var parent:* = tree.getParentItem(item);
var p:int = tree.getItemIndex(parent);
var i:int = tree.getItemIndex(item);
var index:int = i - p - 1;
tree.dataDescriptor.removeChildAt(parent, item, index);
Almoust the same, but it works better for me.
Here is a way to remove leaf nodes with the MX Tree using the dataDescriptor.
var parent:Object = tree.getParentItem(tree.selectedItem);
var p:int = tree.itemRendererToIndex(tree.itemToItemRenderer(parent))
var i:int = tree.itemRendererToIndex(tree.itemToItemRenderer(tree.selectedItem))
tree.dataDescriptor.removeChildAt(parent,tree.selectedItem,i - p - 1);
you can use this as your removal function:
private function removeEmployee():void {
var node:XML = XML(tree.selectedItem);
if( node == null ) return;
var children:XMLList = XMLList(node.parent()).children();
for(var i:Number=0; i < children.length(); i++) {
if( children[i].#name == node.#name ) {
delete children[i];
}
}
}