9 Replies Latest reply on Sep 28, 2009 11:34 AM by Ned Murphy

    Using Imported XML at Runtime.

    raslate Level 1

      I am creating an animation that will be used in 60 or so countries so messages will have to be translated in-country.  From what I understand, using XML is the best way to do this.  So, I created the following XML file (number of items reduced for space) using notepad:

       

      <?xml version="1.0"?>
      <displays name="Transfer Case Messages">
            <message text="Click on the igntion switch to advance the switch one position." messageNumber = "0"/>
            <message text="Please use the brake pedal hold button to shift into and out of Park (P)." messageNumber = "1"/>
            <message text="Speed is limited in 4WD LOW." messageNumber = "2"/>
      </displays>

       

      Then, I used the following code to load the XML file and verify that it loads:

       

      tCaseContent = new XML();
      tCaseContent.ignoreWhite = true;
      tCaseContent.load("transfercase.xml");
      tCaseContent.onLoad = function(success) {
            if(success) {
                  display.text = "Messages have loaded successfully!";
            }
      }

       

      According to my dispay, the message load correctly.

       

      Then, I tried to use the following code to assign variable names to the messages:

       

      for (i=0; tCaseContent.firstChild.childNodes.length; i++)   {
           message[i] = tCaseContent.firstChild.childNodes[i].attributes.message;
      }

       

      Unfortunately, when I try to use a message variable I get undefined.  I'm guessing that I'm not hitting my target in the XML file, but I don't know why.  Any help would be greatly appreciated!

        • 1. Re: Using Imported XML at Runtime.
          Ned Murphy Adobe Community Professional & MVP

          I am not really sure what you are after for storing in your message array, but as I see it, your messge nodes are the childNodes, and the attributes are the text and messageNumber values...

           

          messages[i] = tCaseContent.firstChild.childNodes[i].attributes.text;

           

          Using the preceding line should put the text values into the array.  If you want to store both the text and messageNumber values in the array, you can add them as an object...

           

          messages[i] = { msgText:tCaseContent.firstChild.childNodes[i].attributes.text, msgNum:tCaseContent.firstChild.childNodes[i].attributes.messageNumber} ;

           

          You should also be aware that your for loop won't work as you show it because your second argument is incomplete.  One other thing, you may want to avoid using AS code words in your xml and other code (message and text are two such words)

          1 person found this helpful
          • 2. Re: Using Imported XML at Runtime.
            raslate Level 1

            I'm not sure I understand why the second argument is incomplete.  Should it read:

             

            i=tCaseContent.firstChild.childNodes.length

             

            Thanks for the help.  I really appreciate it!

            • 3. Re: Using Imported XML at Runtime.
              Ned Murphy Adobe Community Professional & MVP

              for (i=0; tCaseContent.firstChild.childNodes.length; i++)   {

              should be...

               

              for (i=0; i<tCaseContent.firstChild.childNodes.length; i++)   {

              1 person found this helpful
              • 4. Re: Using Imported XML at Runtime.
                raslate Level 1

                Okay, I changed my xml to get rid of bad words . . . .

                 

                <?xml version="1.0"?>
                <displays name="Transfer Case Messages">
                <direction blah="Click on the igntion switch to advance the switch one position." directionNumber = "0"/>
                <direction blah="Please use the brake pedal hold button to shift into and out of Park (P)." directionNumber = "1"/>

                 

                And I changed my code, but it still doesn't work:

                 

                tCaseContent = new XML();
                tCaseContent.ignoreWhite = true;
                tCaseContent.load("transfercase.xml");
                tCaseContent.onLoad = function(success) {
                if(success) {
                  display.text = "Messages have loaded successfully!";
                }
                }

                for (i=0; 1<tCaseContent.firstChild.childNodes.length; i++){
                blah[i] = tCaseContent.firstChild.childNodes[i].attributes.blah;
                }
                trace (blah1);

                 

                But it still doesn't work.   I think I'm gonna cry.

                • 5. Re: Using Imported XML at Runtime.
                  Ned Murphy Adobe Community Professional & MVP

                  You need to move that for loop inside your if(success) conditional.... otherwise that code will execute before the file is even loaded.  Also, now you have

                   

                  for (i=0; 1<tCaseContent.firstChild.childNodes.length; i++){

                   

                  the 1 should be an i

                  1 person found this helpful
                  • 6. Re: Using Imported XML at Runtime.
                    raslate Level 1

                    Geez,

                     

                    Between my poor typing and poor coding, this is getting difficult.  Okay, here is what I have now:

                     

                    tCaseContent = new XML();
                    tCaseContent.ignoreWhite = true;
                    tCaseContent.load("transfercase.xml");
                    tCaseContent.onLoad = function(success) {
                          if(success) {
                                display.text = "Messages have loaded successfully!";
                          }
                          for (i=0; i<tCaseContent.firstChild.childNodes.length; i++){
                               blah[i] = tCaseContent.firstChild.childNodes[i].attributes.blah;
                          }
                    }


                    trace (blah1);

                     

                    Still no success.

                     

                    Am I hopeless?

                    • 7. Re: Using Imported XML at Runtime.
                      Ned Murphy Adobe Community Professional & MVP

                      As far as I can see you don't have a "blah1" to trace, and if you did, it is still being executed before the file is loaded.  You do not want to try to deal with the loaded data until after it is loaded (after success).  Just because you place the code later in the page doesn't make it execute after what precedes it... code executes as fast as it can... the onLoad function waits for loading to complete, but nothing else does.

                       

                      var blahs = new Array();

                       

                      tCaseContent = new XML();
                      tCaseContent.ignoreWhite = true;
                      tCaseContent.load("transfercase.xml");
                      tCaseContent.onLoad = function(success) {
                            if(success) {
                                  display.text = "Messages have loaded successfully!";
                                   for (i=0; i<tCaseContent.firstChild.childNodes.length; i++)   {
                                        blahs[i] = tCaseContent.firstChild.childNodes[i].attributes.blah;
                                         trace(i+" "+blahs[i]);
                                   }

                                   trace(blahs);
                            }
                      }

                      • 8. Re: Using Imported XML at Runtime.
                        raslate Level 1

                        Thanks for being so patient!

                         

                        I was about to write one more question when I realized that to call the something out of the array, i couldn't just ask for blahs0, it had to be blahs[0].

                         

                        I think I have all of the bugs worked out of this animation now!

                        • 9. Re: Using Imported XML at Runtime.
                          Ned Murphy Adobe Community Professional & MVP

                          You're welcome