Link to home
Start Free TrialLog in
Avatar of kate_nixon
kate_nixon

asked on

reading XML into an array


I am trying to read some XML into an array and then use one of the variables in that array in a button to jump to a URL.  Can anyone see what I am doing wrong?
I am using Flash 5

In the first cell of the timeline of scene 1:

Function student(name, link, linktitle){
     this.name = name;
     this.link = link;
     this.linktitle = linktitle;
}

function readXML(){
var e = this.firstchild;
if (e.nodename == "JANE"){
     e = e.firstChild;
    jane = new Array();
     while (e != null) {
            var s = new student(e.attributes.name,
                             e.attributes.link,
                              e.attributes.linktitle);
           jane.push(s);
var k  = e.attributes.link
         e = e.nextsibling;

    }
       
}}


// read xml and display when loaded
moXML = new XML();  
moXML.onLoad = readXML;
moXML.load("Linksfl.xml");
stop();

the linksfl.xml file is in the same directory as the fla file

on the button:
on (release) {

     getURL (jane[0].link);
}

the XML:


<JANE>
<STUDENT NAME="Kate Nixon" LINK="http://www.uni-of-life.com" LINKTITLE="Kate's Site" />
</JANE>

when I click on the button nothing happens and it seems that there is nothing in the array although it definitely seems to be reading the XML file earlier.

Thanks

Kate
Avatar of Zeffer
Zeffer
Flag of New Zealand image

I think the problem is in the button.
getURL returns information to a browser window not into the flash player and so you need to specify a window.

try..

on (release) {

    getURL (jane[0].link,"_blank");
}

Z
Avatar of kate_nixon
kate_nixon

ASKER

This doesnt cure the problem.  

The button works when you put a variable in - it just isnt working with the array.  I think there must be something wrong with the array itself.
here's a thought.. arrays are zero based so your link, being the second element, is 1..not 0..

(jane[1].link)


Z
kate,

Let add one more record in your xml like this

<JANE>
<STUDENT NAME="Kate Nixon" LINK="http://www.uni-of-life.com" LINKTITLE="Kate's Site" />
<STUDENT NAME="Yahoo" LINK="http://Yahoo.com" LINKTITLE="Yahoo Site" />
</JANE>

Also add trace in your function 'student' like this and see results in output window.

Function student(name, link, linktitle){
    trace(name);
    this.name = name;
    trace(link);
    this.link = link;
    trace(linktitle);
    this.linktitle = linktitle;
}


You will find that there is bug in function 'readXML'. It is not reading XML properly without xml parser.(you are getting wrong results because of spaces).

Let modified your function 'readXML' as follows to get proper results.

declare array outside this function to get the value anywhere in this movie.

var jane = new Array();

function readXML(){
 mainTag = new XML();
 mainTag = this.firstChild;
 if (mainTag.nodeName.toLowerCase() == "jane") {
  flist = mainTag.childNodes;
  for (i=0; i<=flist.length; i++) {
   if (flist[i].nodeName.toLowerCase() == "student") {
      var s = new student(flist[i].attributes.name,flist[i].attributes.link,flist[i].attributes.linktitle);
      jane.push(s);
   }
  }
 }
}

Now modify button script as follows

on (release) {
    getURL(_root.jane[0].link, "_self");
}

or open it in blank page like this

on (release) {
    getURL(_root.jane[0].link, "_blank");
}

That's it. You will get what you are looking for...

Cheers,
RootDir
ASKER CERTIFIED SOLUTION
Avatar of rootdir
rootdir

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Kate,

  Are you checking my comments?

RootDir
RootDir

I've been too busy to check the site and only just read your comment.  Looks great, will check it out tomorrow (Sunday).

Lots of thanks

Kate
RootDir,

many thanks for this answer which works perfectly.  I am going to ask another related question.  Please take a look for me!
RootDir

Im having trouble accepting your answer - getting an apache error.  Think it is a server problem at experts exchange

bear with me

Kate