5 Replies Latest reply on Feb 3, 2007 3:31 PM by depakchopra

    Load XML with dynamic filename

    shaunie123
      Hi,

      I am trying to create a load xml function where I can send the filname to load to the function, however I get a syntax error with the following function, can anyone see what is wrong here?


      function loadXML(file){
      myXML:XML = new XML();
      myXML.ignoreWhite = true;
      myXML.onLoad = function(success) {
      if (success) {
      // Success
      } else {
      // fail
      }
      };
      myXML.load(path+file);
      }

      Thanks for your advice
        • 1. Re: Load XML with dynamic filename
          depakchopra
          I was trying to do the same thing and came across this great tutorial. I think your syntax is just a little off.

          myXML=new XML();
          myXML.ignoreWhite=true;
          myXML.onLoad=function(ok){
          if(ok){
          //trace('data loaded');
          allData=this.firstChild.childNodes;
          for(i=0;i<allData.length;i++){
          trace(allData .attributes.text);
          }
          }else{
          trace('error');
          }
          }
          myXML.load('gridTest.xml');



          ------------------------------------------------------------------------
          <?xml version="1.0" encoding="iso-8859-1"?>
          <nav>
          <but text="home">
          <heading>You are at Home</heading>
          <content>This is the text that is displayed when click on home</content>
          </but>
          <but text="About Us">
          <heading>You are at the about us section</heading>
          <content>This is the text that is displayed when click on about us</content>
          </but>
          <but text="Contact"></but>
          <heading>You are at Contact</heading>
          <content>This is the text that is displayed when click on Contact</content>

          </nav>
          • 2. Re: Load XML with dynamic filename
            abeall Level 3
            myXML:XML = new XML();

            should be:

            var myXML:XML = new XML();
            • 3. Re: Load XML with dynamic filename
              shaunie123 Level 1
              Thanks for your replies, so just t oconfirm, its ok to have a function within a function, because it still doesnt seem to be working...
              • 4. Re: Load XML with dynamic filename
                abeall Level 3
                > its ok to have a function within a function, because it still doesnt seem to be
                > working...

                Yes. And actually, in your case, you don't actually have a function inside a function, you are defining a function within a function, but the function being defined is outside of the parent function(being defined on the XML object). Shouldn't be a problem either way.

                Use trace() a lot to see if Flash succesfully gets to certain points, and if Flash is 'seeing' what you expect it be seeing. Example:
                trace(path+file)
                • 5. Re: Load XML with dynamic filename
                  depakchopra Level 1
                  I know that what I just gave you works.
                  I just cut and pasted what I put there back into flash and xml pages on my site. If you are getting a popup window with the names Contact, Home, and About Us, its working. And according to your question that is what you were trying to do. Did you want something else?