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 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