Results 1 to 9 of 9
Thread: XML to Array
-
June 14th, 2010, 12:30 PM #1125Registered User
postsXML to Array
Hello!
I need to build an array based on this XML structure
So that it would give me something like the image attached.Code:<student> <name></name> <pic></pic> </student> <student> <name></name> <pic></pic> <pic></pic> <pic></pic> </student> <student> <name></name> <pic></pic> <pic></pic> </student>
The complexity comes from the fact a "student" can have several "pic".
In my actual for loop, I get errors or only partial results.
Your ideas would be greatly appreciated.
I need to be able to always trace a given pic belongs to the student's name.
Many many thanks!
-
June 14th, 2010, 12:39 PM #2
I'm not sure of the context of your application, but accessing the student's and their pics via XML is going to be easier than pushing it into an array and accessing it there. Paste this on to the first frame of a new FLA:
PHP Code:var xml:XML = <data>
<student>
<name>aaa</name>
<pic>p1</pic>
</student>
<student>
<name>bbbb</name>
<pic>p1</pic>
<pic>p2</pic>
<pic>p3</pic>
</student>
<student>
<name>ccccc</name>
<pic>p1</pic>
<pic>p2</pic>
</student>
<student>
<name>ddddd</name>
<pic>p1</pic>
</student>
<student>
<name>eeee</name>
<pic>p1</pic>
<pic>p2</pic>
<pic>p3</pic>
<pic>p4</pic>
</student>
</data>
var total:int = xml.student.length();
for(var i:int=0; i<total; i++) {
var pics:int = xml.student[i].pic.length();
for(var j:int=0; j<pics; j++) {
// you can chose to create objects with these if you need to or push them into an array
trace("ID: "+i);
trace("NAME: "+xml.student[i].name.toString());
trace("PIC: "+xml.student[i].pic[j].toString());
trace("\n\n");
}
}
-
June 14th, 2010, 12:44 PM #36Registered User
postsAssuming myXML is your xml variable...
If you want to have named keys you need to use an object and you'll need to use the .name() method for XML.PHP Code:var myArray:Array = new Array();
for(var i:int = 0; i<myXML.length(); i++){
var temp:Array = new Array();
for(var j:int=0; j<myXML[i].children().length(); j++){
temp.push(myXML[i].children()[j]);
}
myArray.push(temp);
}
-
June 14th, 2010, 01:23 PM #4125Registered User
postsCreatify, thanks!
I've tried your solution, when tracing, it seems just perfect, but I can't create a 2D array based on this.
When trying so, I got only one pic instead of several, ...
Your example traces ID0, ID1, ID2, ID3, ID3, ID3
Does the problem dont come from here (shoud it be ID3, ID4, ID5 .... ??)
Or please tell me how I should be able to say a selected pic belong to the right student.
The thing is that I will hover with mouse on a pic, I will get the id with (e.target.id) and I d'like to browse the created 2D array to find the student name.
Thanks!
-
June 14th, 2010, 01:41 PM #54,314ಠ_ಠ
postsYou should probably just be instantiating the buttons using the XML. Something like this:
Code:var students:XMLList = myXML.student; for each(var student:XML in myXML) { var name:String = String(student.name); var pic:String = String(student.pic); var studentButton:StudentButton = new StudentButton(name, pic); studentButton.addEventListener(MouseEvent.CLICK, doSomething); } function doSomething(event:MouseEvent):void { var button:StudentButton = event.target as StudentButton; trace("Name is", button.name, "pic is", button.pic); }
-
June 14th, 2010, 01:45 PM #6125Registered User
postsAnogar,
Thanks for your message, but my project is way more complicated than this...that's why I would need a 2D array. No offence ofcourse, but I took other people opinion, and they said that was I needed...but I can't code it right.
Any idea ?
Thank you
-
June 14th, 2010, 02:11 PM #74,314ಠ_ಠ
postsYou almost never need a 2D array. I doubt you need one for whatever you're working on.
-
June 14th, 2010, 02:13 PM #8125Registered User
postsI seriously am doubting too...
But if you look my XML and image array on the top, how would you code to get this XML into one or 2 linkable arrays....
Lost, ...
Thank you!
-
June 14th, 2010, 02:16 PM #94,314ಠ_ಠ
postsI'd still do it in a button class for your example. Something like this:
You could also just tie each pic to a name, like:Code:var students:XMLList = myXML.student; for each(var student:XML in myXML) { var name:String = String(student.name); var pics:XMLList = student.pic; var picList:Array = []; for each(var pic:XML in pics) { picList.push(pic); } var studentButton:StudentButton = new StudentButton(name, picList); studentButton.addEventListener(MouseEvent.CLICK, doSomething); } function doSomething(event:MouseEvent):void { var button:StudentButton = event.target as StudentButton; trace("Name is", button.name, "pics:", button.picList); }
If it's picture focused instead of student focused, just reverse the class example to reflect that.Code:var students:XMLList = myXML.student; var picNameMap:Dictionary = new Dictionary(); for each(var student:XML in myXML) { var name:String = String(student.name); var pics:XMLList = student.pic; for each(var pic:XML in pics) { picNameMap[pic] = name; } }Last edited by Anogar; June 14th, 2010 at 02:20 PM.
Similar Threads
-
Simple XML login
By tim_bwfc in forum ActionScript 3Replies: 4Last Post: March 2nd, 2010, 03:44 PM -
Trouble loading xml images!
By stephsh in forum ActionScript 3Replies: 5Last Post: October 9th, 2009, 04:32 AM -
document class information not working
By bobcahill in forum ActionScript 3Replies: 0Last Post: November 20th, 2008, 06:05 PM -
passing XML to array problems
By bananha in forum Flash IDEReplies: 1Last Post: October 18th, 2004, 11:22 AM -
xml database and creating menus from created array object
By cocas in forum Flash IDEReplies: 0Last Post: October 11th, 2004, 08:44 PM

Reply With Quote

Bookmarks