Loading XML into an Array

  • Lord Sivell
  • Novice
  • Novice
  • Lord Sivell
  • Posts: 32
  • Loc: Australia

Post 3+ Months Ago

Hey,

I've got an xml file with data in it that I need to load into an Array. The problem is that I don't know how many elements there'll be or how many child nodes they have, or how many child nodes they have etc. This is what I have so far:

The xml file:
Code: [ Select ]
<stage name="News" file="news"></stage>
<stage name="Gallery" file="gallery">
    <stage name="Flash" file="flash">
        <stage name="Radical Racer" file="radical_racer"></stage>
        <stage name="Spiral" file="spiral"></stage>
        <stage name="Asteroid Field" file="asteroid_field"></stage>
    </stage>
    <stage name="Images" file="images">
        <stage name="Forest" file="forest"></stage>
        <stage name="Cat" file="cat"></stage>
        <stage name="Orange" file="orange"></stage>
    </stage>
</stage>
<stage name="Downloads" file="downloads">
    <stage name="Desktop" file="desktop">
        <stage name="Dark Blue" file="dark_blue"></stage>
        <stage name="Fire" file="fire"></stage>
    </stage>
    <stage name="Icons" file="icons">
        <stage name="Mac" file="mac"></stage>
        <stage name="PC" file="pc"></stage>
    </stage>
</stage>
<stage name="Contact" file="contact"></stage>
  1. <stage name="News" file="news"></stage>
  2. <stage name="Gallery" file="gallery">
  3.     <stage name="Flash" file="flash">
  4.         <stage name="Radical Racer" file="radical_racer"></stage>
  5.         <stage name="Spiral" file="spiral"></stage>
  6.         <stage name="Asteroid Field" file="asteroid_field"></stage>
  7.     </stage>
  8.     <stage name="Images" file="images">
  9.         <stage name="Forest" file="forest"></stage>
  10.         <stage name="Cat" file="cat"></stage>
  11.         <stage name="Orange" file="orange"></stage>
  12.     </stage>
  13. </stage>
  14. <stage name="Downloads" file="downloads">
  15.     <stage name="Desktop" file="desktop">
  16.         <stage name="Dark Blue" file="dark_blue"></stage>
  17.         <stage name="Fire" file="fire"></stage>
  18.     </stage>
  19.     <stage name="Icons" file="icons">
  20.         <stage name="Mac" file="mac"></stage>
  21.         <stage name="PC" file="pc"></stage>
  22.     </stage>
  23. </stage>
  24. <stage name="Contact" file="contact"></stage>


The first frame:
Code: [ Select ]
structXML = new XML();
structXML.ignoreWhite = true;
structXML.load("struct.xml");
structXML.onLoad = function() {
    _root.gotoAndStop(2);
};
stop();
  1. structXML = new XML();
  2. structXML.ignoreWhite = true;
  3. structXML.load("struct.xml");
  4. structXML.onLoad = function() {
  5.     _root.gotoAndStop(2);
  6. };
  7. stop();


The second frame:
Code: [ Select ]
this.createArray = function(levelNum, targetArray, targetXML) {
    this["cNum"+levelNum] = targetXML;
    for (this["iNum"+levelNum]=0; this["iNum"+levelNum]<this["cNum"+levelNum].childNodes.length; this["iNum"+levelNum]++) {
        targetArray[this["iNum"+levelNum]] = new Object();
        targetArray[this["iNum"+levelNum]].name = this["cNum"+levelNum].firstChild.attributes.name;
        targetArray[this["iNum"+levelNum]].file = this["cNum"+levelNum].firstChild.attributes.file;
        targetArray[this["iNum"+levelNum]].children = new Array();
        createArray(levelNum+1, targetArray[this["iNum"+levelNum]].children, this["cNum"+levelNum].firstChild);
        // problem?
        this["cNum"+levelNum] = this["cNum"+levelNum].nextSibling;
        //
    }
};
_root.structArray = new Array();
createArray(0, _root.structArray, _root.structXML);
  1. this.createArray = function(levelNum, targetArray, targetXML) {
  2.     this["cNum"+levelNum] = targetXML;
  3.     for (this["iNum"+levelNum]=0; this["iNum"+levelNum]<this["cNum"+levelNum].childNodes.length; this["iNum"+levelNum]++) {
  4.         targetArray[this["iNum"+levelNum]] = new Object();
  5.         targetArray[this["iNum"+levelNum]].name = this["cNum"+levelNum].firstChild.attributes.name;
  6.         targetArray[this["iNum"+levelNum]].file = this["cNum"+levelNum].firstChild.attributes.file;
  7.         targetArray[this["iNum"+levelNum]].children = new Array();
  8.         createArray(levelNum+1, targetArray[this["iNum"+levelNum]].children, this["cNum"+levelNum].firstChild);
  9.         // problem?
  10.         this["cNum"+levelNum] = this["cNum"+levelNum].nextSibling;
  11.         //
  12.     }
  13. };
  14. _root.structArray = new Array();
  15. createArray(0, _root.structArray, _root.structXML);


I think that the only problem is that the levelNum variable is changed by the createArray function call before the nextSibling can be set.

Any help whould be great.
Thx.
Sivell.
  • Anonymous
  • Bot
  • No Avatar
  • Posts: ?
  • Loc: Ozzuland
  • Status: Online

Post 3+ Months Ago

  • digitalMedia
  • a.k.a. dM
  • Genius
  • User avatar
  • Posts: 5149
  • Loc: SC-USA

Post 3+ Months Ago

I'm not sure what version of Flash you are working with, but if you have acces to it, I highly recommend using Data Components for this. I would use an XML Connector that was bound to a Dataset, and then use a Datagrid bound to that while I was developing so I could see what was going on.

Using the Dataset's iterator, fiter, and sort functions makes for easy manipulation of structured data.

I've also used this scheme to bind Datasets to each other allowing for multiple levels of sub-arrays of data.

These components can save you an incredible amount of effort.

Post Information

  • Total Posts in this topic: 2 posts
  • Users browsing this forum: No registered users and 8 guests
  • You cannot post new topics in this forum
  • You cannot reply to topics in this forum
  • You cannot edit your posts in this forum
  • You cannot delete your posts in this forum
  • You cannot post attachments in this forum
 
 

© 1998-2014. Ozzu® is a registered trademark of Unmelted, LLC.