Skip to main content
Participating Frequently
April 15, 2009
Question

loading xml into separate arrays

  • April 15, 2009
  • 1 reply
  • 1229 views

The first two records of my XML file are below these are two of about 100.


<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<coursesSummary>
<courses>
<year>2009</year>
<quarter>1</quarter>
<region>Central Courses</region>
<coursecode>3634500</coursecode>
<coursetitle>Bancassurance(BIP) 2- Selling Skills Course</coursetitle>
<coursesrun>1</coursesrun>
<maxplaces>10</maxplaces>
<numberattended>9</numberattended>
<splitbyregion>no</splitbyregion>
<centralreport>yes</centralreport>
</courses>
<courses>
<year>2009</year>
<quarter>1</quarter>
<region>Central Courses</region>
<coursecode>3634600</coursecode>
<coursetitle>Bancassurance (BIP) 3-Developing your Business</coursetitle>
<coursesrun>1</coursesrun>
<maxplaces>15</maxplaces>
<numberattended>8</numberattended>
<splitbyregion>no</splitbyregion>
<centralreport>yes</centralreport>
</courses>
</coursesSummary>


<------------------------------------------------------------>

I need to be able to make an associative array of the elements within the <courses> tag.
I need to be able to show mix and match the data from the 9 arrays.

I can get the data into a text box but am unable to make the arrays work. In test
I only get one item of data from the first <courses> tag. the array below just displays 2009

<------------------ .fla below -------------------------------->

var myXML = new XML();
myXML.ignoreWhite = true;
myXML.onLoad = loadCourses;
var intervalID:Number = setInterval(checkLoad, 5, myXML);
myXML.load("Book2data.xml");
function loadCourses(success:Boolean):Void {
if (success) {
//trace("data");
} else {
//trace("no data");
}
}
function loadCourses():Void {
cNodes = this.childNodes[0].childNodes[0];
var yearArray:Array = cNodes.childNodes[0].firstChild.nodeValue;
for (i=0; i<yearArray.length; i++) {
yearArray = cNodes.childNodes[0].firstChild.nodeValue;
}
trace(yearArray);
courseText.text = cNodes.childNodes[0].firstChild.nodeValue;//year
/*courseText.text = cNodes.childNodes[1].firstChild.nodeValue;//quarter
courseText.text = cNodes.childNodes[2].firstChild.nodeValue;//region
courseText.text = cNodes.childNodes[3].firstChild.nodeValue;//coursecode
courseText.text = cNodes.childNodes[4].firstChild.nodeValue;//coursetitle
courseText.text = cNodes.childNodes[5].firstChild.nodeValue;//coursesrun
courseText.text = cNodes.childNodes[6].firstChild.nodeValue;//maximunplaces
courseText.text = cNodes.childNodes[7].firstChild.nodeValue;//numberattended
courseText.text = cNodes.childNodes[8].firstChild.nodeValue;//splitbyregion
courseText.text = cNodes.childNodes[9].firstChild.nodeValue;//centralreport
*/
}

This topic has been closed for replies.

1 reply

RossRitchey
Inspiring
April 17, 2009

The reason you are only getting the first node is because you are only setting the first node ever:

for (i=0; i<yearArray.length; i++) {
     yearArray = cNodes.childNodes[0].firstChild.nodeValue;
}

cNodes.childNodes[0].firstChild.nodeValue will give you the first node, but you are repeating this as many times as yearArray is long

change to:

for (i=0; i<yearArray.length; i++) {
     yearArray = cNodes.childNodes.firstChild.nodeValue;
}

to get the value at each node as you go through the loop.

struglerAuthor
Participating Frequently
April 18, 2009

Thanks you so much for the time and trouble you have taken in sending your answer. As soon as i read it I felt very bad that I had made such a silly error. However on amanding the code as you kindly sugested the problem still remains. I only get a trace on of the first valure 2009.

I have tried guggling the code a bit but with no success. Please could you help further by having another look The revised code is below, the xml is in the first post.

Thanks again

    var cNodes:Array  = this.childNodes[0].childNodes[0];
    var yearArray:Array = cNodes.childNodes[0].firstChild.nodeValue;
   
   
    for (i=0; i<yearArray.length; i++) {
        yearArray = cNodes.childNodes.firstChild.nodeValue;
    }
    trace(yearArray);

April 18, 2009

May I suggest simply populating a course array with course objects:

var theCourses:XML = new XML();
theCourses.ignoreWhite = true;

var courseData:Array = new Array();

theCourses.onLoad = function(success) {
    if(success){
        var l = theCourses.firstChild.childNodes.length;       
        for(var i = 0; i < l; i++){
            var course:Object = new Object();
            course.year = theCourses.firstChild.childNodes.childNodes[0].firstChild.nodeValue;
            course.quarter = theCourses.firstChild.childNodes.childNodes[1].firstChild.nodeValue;
            course.region = theCourses.firstChild.childNodes.childNodes[2].firstChild.nodeValue;
            course.code = theCourses.firstChild.childNodes.childNodes[3].firstChild.nodeValue;
            course.title = theCourses.firstChild.childNodes.childNodes[4].firstChild.nodeValue;
            //etc.
            courseData.push(course);
        }       
    }   
    trace(courseData[1].title);
};

theCourses.load("courses.xml");
stop();