Everybody! This is important. In a few days, these forums will be moving over to using the totally sweet Discourse platform. To ensure this migration happens smoothly with no loss of content, these forums are currently in a read-only mode. I do apologize for the inconvenience.

There is never a good time to turn the forums off for an extended period of time, but I promise the new forums will be a billion times better. I'm pretty sure of it.

See you all on the other side in a few days, and if you have any (non-technical) questions, please e-mail me at kirupa@kirupa.com. For technical questions, try to find a tutorial that corresponds to what you are looking for and post in the comments section of that page.

Cheers,
Kirupa

Results 1 to 7 of 7

Thread: populating 3d array from external xml

  1. #1

    populating 3d array from external xml

    Hello! I have a short xml file. I want to populate it using some kind of loop inside an empty 3D array when the file loads, I want it to look like this:

    Code:
      ["LOL", "Laughing Out Loud", "0"],
      ["OMG", "Oh My God", "0"],
      ["BTW", "By The Way", "0"]
    Here's my XML file:
    Code:
    <questions>
        <question>
        <short>What's the meaning of..</short>
        <meaning>the meaning</meaning>
        <sign>0</sign>
        </question>
    
         <question>
        <short>What's the meaning of..</short>
        <meaning>the meaning</meaning>
        <sign>0</sign>
        </question>
    
         <question>
        <short>What's the meaning of..</short>
        <meaning>the meaning</meaning>
        <sign>0</sign>
        </question>
       
    
    </questions>
    How can I do that?

    Thank you!

  2. #2
    125
    posts
    Registered User
    You just need to nest your arrays. In your example above, your outer array will hold 3 arrays, each of the inner arrays will hold the values. One way you could manually duplicate what I mean using your above example is:
    Code:
    var arr:Array = new Array();
    arr.push(["LOL", "Laughing Out Loud", "0"]);
    arr.push(["OMG", "Oh My God", "0"]);
    arr.push(["BTW", "By The Way", "0"]);
    trace(arr[2][1]); //By The Way

  3. #3
    Quote Originally Posted by Scott64 View Post
    You just need to nest your arrays. In your example above, your outer array will hold 3 arrays, each of the inner arrays will hold the values. One way you could manually duplicate what I mean using your above example is:
    ActionScript Code:
    var arr:Array = new Array();
    arr.push(["LOL", "Laughing Out Loud", "0"]);
    arr.push(["OMG", "Oh My God", "0"]);
    arr.push(["BTW", "By The Way", "0"]);
    trace(arr[2][1]); //By The Way

    Scott thank you but I need to do this with some kind of loop everytime I load the file, because I don't know how long my file will get.
    Does anyone know how to do that?

  4. #4
    125
    posts
    Registered User
    You just use loops. Use one loop for your parent elements and another loop to put the child elements into the parent arrays, all of which are stored in one master array.

  5. #5
    Quote Originally Posted by Scott64 View Post
    You just use loops. Use one loop for your parent elements and another loop to put the child elements into the parent arrays, all of which are stored in one master array.
    I figured as much, but I'm not sure how to build this loop and I would appreciate the help with creating the loop.

  6. #6
    125
    posts
    Registered User
    Something like this would work with the XML provided above:
    Code:
    var xml:XML = 
    <questions>
        <question>
        <short>What&apos;s the meaning of..</short>
        <meaning>the meaning</meaning>
        <sign>0</sign>
        </question>
    
         <question>
        <short>What&apos;s the meaning of..</short>
        <meaning>the meaning</meaning>
        <sign>0</sign>
        </question>
    
         <question>
        <short>What&apos;s the meaning of..</short>
        <meaning>the meaning</meaning>
        <sign>0</sign>
        </question>
       
    
    </questions>;
    
    var questions:Array = new Array();
    for(var i:uint = 0; i < xml.question.length(); i++){
        questions.push([xml.question[i].short, xml.question[i].meaning, xml.question[i].sign]);
    }
    trace(questions); //What's the meaning of..,the meaning,0,What's the meaning of..,the meaning,0,What's the meaning of..,the meaning,0

  7. #7
    Quote Originally Posted by Scott64 View Post
    Something like this would work with the XML provided above:
    ActionScript Code:
    var xml:XML =
    <questions>
    <question>
    <short>What&apos;s the meaning of..</short>
    <meaning>the meaning</meaning>
    <sign>0</sign>
    </question>

    <question>
    <short>What&apos;s the meaning of..</short>
    <meaning>the meaning</meaning>
    <sign>0</sign>
    </question>

    <question>
    <short>What&apos;s the meaning of..</short>
    <meaning>the meaning</meaning>
    <sign>0</sign>
    </question>


    </questions>;

    var questions:Array = new Array();
    for(var i:uint = 0; i < xml.question.length(); i++){
    questions.push([xml.question[i].short, xml.question[i].meaning, xml.question[i].sign]);
    }
    trace(questions); //What's the meaning of..,the meaning,0,What's the meaning of..,the meaning,0,What's the meaning of..,the meaning,0

    Thank you!

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