The forums have permanently moved to forum.kirupa.com. This forum will be kept around in read-only mode for archival purposes. To learn how to continue using your existing account on the new forums, check out this thread.


Results 1 to 9 of 9

Thread: XML to Array

  1. #1

    XML to Array

    Hello!

    I need to build an array based on this XML structure
    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>
    So that it would give me something like the image attached.

    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!
    Attached Thumbnails Attached Thumbnails Click image for larger version. 

Name:	Array2D.JPG 
Views:	18 
Size:	10.7 KB 
ID:	52171  

  2. #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=0i<totali++) {
        var 
    pics:int xml.student[i].pic.length();
        for(var 
    j:int=0j<picsj++) {
            
    // 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");
        }


  3. #3
    Assuming myXML is your xml variable...
    PHP Code:
    var myArray:Array = new Array();
    for(var 
    i:int 0i<myXML.length(); i++){
         var 
    temp:Array =  new Array();
         for(var 
    j:int=0j<myXML[i].children().length(); j++){
              
    temp.push(myXML[i].children()[j]);
         }
         
    myArray.push(temp);

    If you want to have named keys you need to use an object and you'll need to use the .name() method for XML.

  4. #4
    Creatify, 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!

  5. #5
    You 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);
    }

  6. #6
    Anogar,

    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

  7. #7
    You almost never need a 2D array. I doubt you need one for whatever you're working on.

  8. #8
    I 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!

  9. #9
    I'd still do it in a button class for your example. Something like this:

    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);
    }
    You could also just tie each pic to a name, like:

    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;
    	}
    }
    If it's picture focused instead of student focused, just reverse the class example to reflect that.
    Last edited by Anogar; June 14th, 2010 at 02:20 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Home About kirupa.com Meet the Moderators Advertise

 Link to Us

 Credits

Copyright 1999 - 2012