7 Replies Latest reply on Jan 5, 2012 12:54 AM by Gojak

    as2 change XML node via button

    Gojak Level 1

      Hi,

       

      I am a designer learning AS. I would be grateful if someone can help me solve this code.  I need my flash application to be able to switch nodes for certain text fields and movieclip that loads images, when I click on any of 6 simple buttons. Everything works except that part. How to switch nodes? Can't find it anywhere in websearch that buttons are changing nodes, maybe it is called something else and not "switching nodes"? Here is a part of code that I can't solve, it is starting at // PROBLEM part. Thank you!

       

      //-----------AS2

       

      function loadXML(loaded) {
          if (loaded) {
          
              _root.locationX = this.firstChild.childNodes[0].childNodes[0].firstChild.nodeValue;
              _root.nameX = this.firstChild.childNodes[0].childNodes[1].firstChild.nodeValue;
              _root.emailX = this.firstChild.childNodes[0].childNodes[2].firstChild.nodeValue;
              _root.image = this.firstChild.childNodes[0].childNodes[3].firstChild.nodeValue;
      
      //loads text
              location_txt.text = _root.locationX;
              name_txt.text = _root.nameX;
              email_txt.text = _root.emailX;
      //loads image   
              var url:String = _root.image;
              var movieClip:MovieClip = createEmptyMovieClip("movieClip", 0);
              movieClip.loadMovie(url);
              
          
      // PROBLEM:  this BUTTON has to switch values to next node
        
      menu1.onPress = function() {
      xmlData = new XML();
      xmlData.ignoreWhite = true;
      xmlData.onLoad = loadXML;
              _root.locationX = loadXML.firstChild.childNodes[1].childNodes[0].firstChild.nodeValue;
              _root.nameX = loadXML.firstChild.childNodes[1].childNodes[1].firstChild.nodeValue;
              _root.emailX = loadXML.firstChild.childNodes[1].childNodes[2].firstChild.nodeValue;
              _root.image = loadXML.firstChild.childNodes[1].childNodes[3].firstChild.nodeValue;
      }
      menu2.onPress = function() {
      xmlData = new XML();
      xmlData.ignoreWhite = true;
      xmlData.onLoad = loadXML;
              _root.locationX = loadXML.firstChild.childNodes[2].childNodes[0].firstChild.nodeValue;
              _root.nameX = loadXML.firstChild.childNodes[2].childNodes[1].firstChild.nodeValue;
              _root.emailX = loadXML.firstChild.childNodes[2].childNodes[2].firstChild.nodeValue;
              _root.image = loadXML.firstChild.childNodes[2].childNodes[3].firstChild.nodeValue;
      }
      /// ...etc. --- 6 simple buttons, so far it is not working
      
      
      } else {
      content = "file not loaded!";
      }
      }
      

       

       

       

      ///+++++++++++++++++  XML file: CDA is for CDA button, PLM is for PLM button, etc.

       

      <?xml version="1.0" encoding="utf-8" ?>
      <dots>
          <DATA>
                <location>Empty location</location>
                <name>Empty Name</name>
                <email>Empty email address</email>
                <image>Contacts/img/empty.jpg</image>
      
          </DATA>
          <CDA>
                <location>Annemasse - CDA M&T Team</location>
                <name>John Doe CDA</name>
                <email>111@111.com</email>
                <image>Contacts/img/img1.jpg</image>
      
          </CDA>
          <PLM>
                <location>Annemasse - PLM key user</location>
                <name>John Doe PLM</name>
                <email>222@111.com</email>
                          <img>Contacts/img/img2.jpg</img>
      
          </PLM>
          <CAD>
                <location>Annemasse - CAD key user</location>
                <name>John Doe CAD</name>
                <email>333@111.com</email>
                          <img>Contacts/img/img3.jpg</img>
      
          </CAD>
          <CAE>
                <location>Annemasse - CAE key user</location>
                <name>John Doe CAE</name>
                <email>444@111.com</email>
                          <img>Contacts/img/aimg4.jpg</img>
      
          </CAE>
          <PMT>
                <location>Annemasse - PMT key user</location>
                <name>John Doe PMT</name>
                <email>555@111.com</email>
                          <img>Contacts/img/img5.jpg</img>
      
          </PMT>
          <engineering>
                <location>Annemasse - ENGINEERING IS CONTACTS</location>
                <name>John Doe ENG</name>
                <email>666@111.com</email>
                          <img>Contacts/img/img6.jpg</img>
      
          </engineering>
      </dots>
      
        • 1. Re: as2 change XML node via button
          kglad Adobe Community Professional & MVP

          are you using movieclip buttons or simple buttons?

          • 2. Re: as2 change XML node via button
            Gojak Level 1

            Hi,

            I use simple custom buttons. I can replace them with movie clips if you have a solution for that

            Thanks!

            • 3. Re: as2 change XML node via button
              kglad Adobe Community Professional & MVP

              use movieclip buttons and load your xml once:

               

              var movieClip:MovieClip = createEmptyMovieClip("movieClip", 0);

              var xmlData:XML;
              xmlData.ignoreWhite = true;
              xmlData.onLoad = loadXML;

              xmlData.load(whatever.xml);

               

              function loadXML() {
              xmlData=this;

              }

               

              for(var i:Number=0;i<6;i++){  // assuming you have menu0,menu1,...,menu5

              this["menu"+i].ivar=i;

              this["menu"+i].onRelease=changeF;


              function changeF(){


                      _root.locationX = xmlData.firstChild.childNodes[this.ivar].childNodes[0].firstChild.nodeValue;
                      _root.nameX = this.firstChild.childNodes[this.ivar].childNodes[1].firstChild.nodeValue;
                      _root.emailX = this.firstChild.childNodes[this.ivar].childNodes[2].firstChild.nodeValue;
                      _root.image = this.firstChild.childNodes[this.ivar].childNodes[3].firstChild.nodeValue;

              //loads text
                      location_txt.text = _root.locationX;
                      name_txt.text = _root.nameX;
                      email_txt.text = _root.emailX;
              //loads image  
                      var url:String = _root.image;
                     
                      movieClip.loadMovie(url);

              }    

              • 4. Re: as2 change XML node via button
                Gojak Level 1

                Thanks, That's it! Only now movie clip with image is not loading : _root.image = this.firstChild.childNodes[this.ivar].childNodes[3].firstChild.nodeValue;

                I am getting; Error opening URL 'file:///H|/Source folder/undefined' --- it can't trace node to  Source folder/Contacts/img/imagename.jpg

                I'll see more about this and post back.

                 

                code:

                 

                var movieClip:MovieClip = createEmptyMovieClip("movieClip", 0);

                var xmlData:XML;

                xmlData.ignoreWhite = true;

                xmlData.onLoad = loadXML;

                xmlData.load("Contacts\Empty.xml");

                function loadXML() {

                xmlData=this;

                }

                 

                for(var i:Number=0;i<7;i++){  // assuming you have menu0,menu1,...,menu5

                this["menu"+i].ivar=i;

                this["menu"+i].onRelease=changeF;

                }

                 

                function changeF(){

                 

                        _root.locationX = xmlData.firstChild.childNodes[this.ivar].childNodes[0].firstChild.nodeValue;

                        _root.nameX = xmlData.firstChild.childNodes[this.ivar].childNodes[1].firstChild.nodeValue;

                        _root.emailX = xmlData.firstChild.childNodes[this.ivar].childNodes[2].firstChild.nodeValue;

                        _root.image = this.firstChild.childNodes[this.ivar].childNodes[3].firstChild.nodeValue;

                       

                        location_txt.text = _root.locationX;

                        name_txt.text = _root.nameX;

                        email_txt.text = _root.emailX;

                        var url:String = _root.image;

                        var movieClip1:MovieClip = createEmptyMovieClip("movieClip1", 0);

                        movieClip1.loadMovie(url);

                       

                }

                 

                ////////////////////////

                 

                 

                ///CLOSE BTN

                 

                closeMe.onPress = function() {

                closeMe._x = -47.25;

                contact._x = -400;

                movieClip._x = -390;

                location_txt._x = -278;

                name_txt._x = -224;

                email_txt._x = -224;

                copyMe._x = -235;

                }

                 

                 

                ///COPYME

                // ....

                 

                 

                ///LOCATIONS ON CLICK

                 

                Abbeville.onPress = function() {

                xmlData = new XML();

                xmlData.ignoreWhite = true;

                xmlData.onLoad = loadXML;

                xmlData.load("Contacts/Abbeville.xml");

                closeMe._x = 517;

                contact._x = 165;

                movieClip._x = 180;

                location_txt._x = 292;

                name_txt._x = 346;

                email_txt._x = 346;

                copyMe._x = 333;

                }

                 

                Annemasse.onPress = function() {

                xmlData = new XML();

                xmlData.ignoreWhite = true;

                xmlData.onLoad = loadXML;

                xmlData.load("Contacts/Annemasse.xml");

                closeMe._x = 517;

                contact._x = 165;

                movieClip._x = 180;

                location_txt._x = 292;

                name_txt._x = 346;

                email_txt._x = 346;

                copyMe._x = 333;}

                • 5. Re: as2 change XML node via button
                  Gojak Level 1

                  My mistake, this part works works flawlessly. Thank you!!!

                  Only problem now is that I have a couple of location clips that should load another xml on click. It works but it doesn't refresh when I select any of them( it displays text from previous XML). It refreshes only when clicking on side buttons menu1, menu2... (function created via script above). I hope you can understand from my description what is the problem

                   

                   

                  CODE:

                   

                   

                  var xmlData:XML;

                  xmlData.ignoreWhite = true;

                  xmlData.onLoad = loadXML;

                  xmlData.load("Contacts\Empty.xml");

                  function loadXML() {

                  xmlData=this;

                  }

                   

                  for(var i:Number=0;i<7;i++){ 

                  this["menu"+i].ivar=i;

                  this["menu"+i].onRelease=changeF;

                  }

                   

                  function changeF(){

                   

                          _root.locationX = xmlData.firstChild.childNodes[this.ivar].childNodes[0].firstChild.nodeValue;

                          _root.nameX = xmlData.firstChild.childNodes[this.ivar].childNodes[1].firstChild.nodeValue;

                          _root.emailX = xmlData.firstChild.childNodes[this.ivar].childNodes[2].firstChild.nodeValue;

                          _root.image = xmlData.firstChild.childNodes[this.ivar].childNodes[3].firstChild.nodeValue;

                   

                          location_txt.text = _root.locationX;

                          name_txt.text = _root.nameX;

                          email_txt.text = _root.emailX;

                          var url:String = _root.image;

                          var movieClip1:MovieClip = createEmptyMovieClip("movieClip1", 0);

                          movieClip1.loadMovie(url);

                   

                  }

                   

                  ////////////////////////

                   

                   

                  ///CLOSE BTN

                   

                  closeMe.onPress = function() {

                  closeMe._x = -47.25;

                  contact._x = -400;

                  movieClip1._x = -390;

                  location_txt._x = -278;

                  name_txt._x = -224;

                  email_txt._x = -224;

                  copyMe._x = -235;

                  }

                   

                  contact.onRollOver = function() {

                  tooltip._visible = false;

                  }

                   

                  world.onPress = function() {

                  closeMe._x = -47.25;

                  contact._x = -400;

                  movieClip1._x = -390;

                  location_txt._x = -278;

                  name_txt._x = -224;

                  email_txt._x = -224;

                  copyMe._x = -235;

                  }

                   

                  ///COPYME

                   

                   

                   

                  ///LOCATIONS ON CLICK

                   

                  Abbeville.onPress = function() {

                  xmlData = new XML();

                  xmlData.ignoreWhite = true;

                  xmlData.onLoad = loadXML;

                  xmlData.load("Contacts/Abbeville.xml");

                  closeMe._x = 517;

                  contact._x = 165;

                  movieClip1._x = 180;

                  location_txt._x = 292;

                  name_txt._x = 346;

                  email_txt._x = 346;

                  copyMe._x = 333;

                  }

                   

                  Annemasse.onPress = function() {

                  xmlData = new XML();

                  xmlData.ignoreWhite = true;

                  xmlData.onLoad = loadXML;

                  xmlData.load("Contacts/Annemasse.xml");

                  closeMe._x = 517;

                  contact._x = 165;

                  movieClip1._x = 180;

                  location_txt._x = 292;

                  name_txt._x = 346;

                  email_txt._x = 346;

                  copyMe._x = 333;}

                   

                  Benarous.onPress = function() {

                  xmlData = new XML();

                  xmlData.ignoreWhite = true;

                  xmlData.onLoad = loadXML;

                  xmlData.load("Contacts/Benarous.xml");

                  closeMe._x = 517;

                  contact._x = 165;

                  movieClip1._x = 180;

                  location_txt._x = 292;

                  name_txt._x = 346;

                  email_txt._x = 346;

                  copyMe._x = 333;}

                   

                  Bietigheim.onPress = function() {

                  xmlData = new XML();

                  xmlData.ignoreWhite = true;

                  xmlData.onLoad = loadXML;

                  xmlData.load("Contacts/Bietigheim.xml");

                  closeMe._x = 517;

                  contact._x = 165;

                  movieClip1._x = 180;

                  location_txt._x = 292;

                  name_txt._x = 346;

                  email_txt._x = 346;

                  copyMe._x = 333;}

                   

                  Creteil.onPress = function() {

                  xmlData = new XML();

                  xmlData.ignoreWhite = true;

                  xmlData.onLoad = loadXML;

                  xmlData.load("Contacts/Creteil.xml");

                  closeMe._x = 517;

                  contact._x = 165;

                  movieClip1._x = 180;

                  location_txt._x = 292;

                  name_txt._x = 346;

                  email_txt._x = 346;

                  copyMe._x = 333;}

                   

                  Erdweg.onPress = function() {

                  xmlData = new XML();

                  xmlData.ignoreWhite = true;

                  xmlData.onLoad = loadXML;

                  xmlData.load("Contacts/Erdweg.xml");

                  closeMe._x = 517;

                  contact._x = 165;

                  movieClip1._x = 180;

                  location_txt._x = 292;

                  name_txt._x = 346;

                  email_txt._x = 346;

                  copyMe._x = 333;}

                  • 6. Re: as2 change XML node via button
                    kglad Adobe Community Professional & MVP

                    use:

                     

                    Abbeville.onPress = function() {

                    xmlData.load("Contacts/Abbeville.xml");

                    closeMe._x = 517;

                    contact._x = 165;

                    movieClip1._x = 180;

                    location_txt._x = 292;

                    name_txt._x = 346;

                    email_txt._x = 346;

                    copyMe._x = 333;

                    }

                     

                    etc

                    • 7. Re: as2 change XML node via button
                      Gojak Level 1

                      Hi,

                      I already did that and it is not woking with new provided script. It was working without that dynamic function for menu buttons.

                      . I will try a workaround: I will create a movieclip that goes over text display area (it will be like false empty fields) and that way when user cliks on menu1 - menu6 buttons it will move itself and display updated content below in text area. I need to be flexible

                      1 person found this helpful