6 Replies Latest reply on Jun 22, 2009 10:22 AM by Ned Murphy

    Trouble with Variables

    BrianStapleton Level 1

      After loading data in from an asp page I assigned the following variables.

       

      var jobRoot = this.firstChild;
      var job = jobRoot.firstChild;
      var jobPosition = job.childNodes[1].firstChild.nodeValue;
      var jobLocation = job.childNodes[2].firstChild.nodeValue;

       

      When I export my swf I only get "undefined" where the job position and location information should be.


      detailText.text = "Job Position" + newline + jobPosition + newline + "Location" + newline + jobLocation;

       

      Can anyone see where I'm going wrong? Thanks

        • 1. Re: Trouble with Variables
          Ned Murphy Adobe Community Professional & MVP

          HAve you traced the values that you parse to be sure what you are expecting to be assigned is being assigned to those vars?  Are those vars defined inside a function while trying to use them outside of it?

          1 person found this helpful
          • 2. Re: Trouble with Variables
            BrianStapleton Level 1

            Hi Ned, thanks for the reply. I tried tracing the values as you suggested and it also came back undefined. So I traced the variable that the asp page is originally loaded into, but that came through fine. So presuming that I'd just incorrectly targeted the nodes, I went about trying various arrangements of addresses (eg. jobRoot.firstChild.childNodes[1].firstChild.nodeValue;) but none worked.

             

            This is the structure of the asp (in XML):

             

            <Vacancies>
            <Job>
            <NID>449</NID>
            <Position>General Operative</Position>
            <Location>Galway</Location>
            </Job>
            </Vacancies>

             

            How would you target the position or location nodes?

            • 3. Re: Trouble with Variables
              BrianStapleton Level 1

              Oh and by the way, the vars are defined in the same function which is calling them into the textbox.

              • 4. Re: Trouble with Variables
                Ned Murphy Adobe Community Professional & MVP

                Brian, I haven't dealt with XML vs AS2 (only AS3)... so I figure it's about time I did something.  So I took a quick look online and found something that works for me and places the data into a data structure that I can more easily work with--the author brags of its efficiency.  I don't know if it will work as nicely for what you have designed so far, but it's worth looking at in any case.  Here is where I found it as a reference for you:  http://www.darronschall.com/weblog/2003/10/better-xml-parsing.cfm

                 

                What this code does is it stores that xml data into an array of objects.  So when the parsing is done, you don't have to deal with nodes and whatnot to figure out where the data is... just an array and the objects within it.  At the end of the first function I put in a trace just to demonstrate accessing the data after parsing is complete.

                 

                Here is a revised version of the code using your xml data structure.  I primarily renamed things, removed some timing related code, and added the trace output of the data:

                 

                vacancy_list_arr = new Array();
                vacancy_list_xml = new XML();
                vacancy_list_xml.ignoreWhite = true;

                 
                vacancy_list_xml.onLoad = function(success) {
                if (success) {
                  var job_xml = vacancy_list_xml.firstChild.firstChild;
                  while (job_xml != null) { // add the job data to our vacancy_list_arr!
                   vacancy_list_arr.push(getJobData(job_xml));
                   job_xml = job_xml.nextSibling;
                  } 
                } else {
                  trace("Error loading vacancy_list.");
                }
                delete vacancy_list_xml;

                for(i=0; i<vacancy_list_arr.length; i++){   // for demo purposes only
                  trace("Job ID: "+vacancy_list_arr[i].NID+" - "+vacancy_list_arr[i].Position+" - "+vacancy_list_arr[i].Location);
                }
                }

                 

                function getJobData(job_xml) { // convert data to object form
                var jobData = new Object();
                var data_xml = new XML();
                data_xml = job_xml.firstChild;
                while (data_xml != null) {
                  jobData[data_xml.nodeName] = data_xml.firstChild.nodeValue;
                  data_xml = data_xml.nextSibling;
                }
                return jobData;
                }

                vacancy_list_xml.load("vacancy_list.xml");

                 

                 

                And here is the xml file content I created to test it:

                 

                <?xml version="1.0"?>
                <Vacancies>
                     <Job>
                          <NID>449</NID>
                          <Position>General Operative</Position>
                          <Location>Galway</Location>
                     </Job>
                     <Job>
                          <NID>550</NID>
                          <Position>Foreign Operative</Position>
                          <Location>Outtatheway</Location>
                     </Job>
                </Vacancies>

                • 5. Re: Trouble with Variables
                  BrianStapleton Level 1

                  WOW!! That worked perfectly. Thanks for your time!

                  • 6. Re: Trouble with Variables
                    Ned Murphy Adobe Community Professional & MVP

                    You're welcome