8 Replies Latest reply on Jul 21, 2006 9:28 AM by Newsgroup_User

    Setting selectedIndex via XML

    catcherintherye
      I have a comboBox that is populated by an XML file. Is there a way to set the selectedIndex to a variable within the XML file?

      For example, my XML data looks like this:

      <villa name="Villa 2" URL=" http://www.URL2.com" default_index="1"/>

      When someone makes a selection from the combo box, I would like the user to be directed to a new page with the default index set correctly. In the above example, if the user chooses "Villa 2" they are directed to the URL.com site, and I want the default in the combo box on the new page to be index 1, which is "Villa 2".

      I can successfully pass the info for the default_index via FlashVars on the new page, but I want to set the index with the XML file and not the HTML file.

      The following line works if I set the default_index variable through FlashVars, but doesn't read it if the default_index is set in the XML.

      comboBox.selectedIndex=default_index

      Any ideas?

      Here's the code that populates the comboBox and sets the default index:

      var myData:XML = new XML ();
      myData.ignoreWhite = true;
      myData.load ("myData.xml");
      myData.onLoad = function (success:Boolean):Void
      {
      if (success)
      {
      var data:XMLNode = this.firstChild;
      var children:Array = data.childNodes;
      var numItems:Number = children.length;

      for (var i:Number = 0; i < numItems; i++)
      {
      var child:XMLNode = children ;
      var name:String = child.attributes.name;
      var URL:String = child.attributes.URL;
      comboBox.addItem (name,URL);
      }
      }comboBox.selectedIndex=default_indx
      }

      Thanks in advance!
        • 1. Re: Setting selectedIndex via XML
          Level 7

          for (var i:Number = 0; i < numItems; i++)
          {
          var child:XMLNode = children ;
          var name:String = child.attributes.name;
          var URL:String = child.attributes.URL;
          comboBox.addItem (name,URL);

          // assumes there will be only one node with a default_index attribute
          // -- if more than one, the last one will be the selected item
          if(child.attributes["default_index"] !=
          undefined){comboBox.selectedIndex=i;}
          }

          other approach:

          <datanode default_index="1"/>
          <villa name="Villa 1" URL=" http://www.URL1.com" />
          <villa name="Villa 2" URL=" http://www.URL2.com" />
          <villa name="Villa 3" URL=" http://www.URL3.com" />
          </datanode>

          var data:XMLNode = this.firstChild;
          var children:Array = data.childNodes;
          var numItems:Number = children.length;

          for (var i:Number = 0; i < numItems; i++)
          {
          var child:XMLNode = children;
          var name:String = child.attributes.name;
          var URL:String = child.attributes.URL;
          comboBox.addItem (name,URL);
          }
          defIdx = Number(data.attributes["default_index"]);
          comboBox..selectedIndex = isNaN(defIdx) ? 0 : defIdx;

          "catcherintherye" <webforumsuser@macromedia.com> wrote in message
          news:e9melv$76l$1@forums.macromedia.com...
          > I have a comboBox that is populated by an XML file. Is there a way to set
          the
          > selectedIndex to a variable within the XML file?
          >
          > For example, my XML data looks like this:
          >
          > <villa name="Villa 2" URL=" http://www.URL2.com" default_index="1"/>
          >
          > When someone makes a selection from the combo box, I would like the user
          to be
          > directed to a new page with the default index set correctly. In the above
          > example, if the user chooses "Villa 2" they are directed to the URL.com
          site,
          > and I want the default in the combo box on the new page to be index 1,
          which is
          > "Villa 2".
          >
          > I can successfully pass the info for the default_index via FlashVars on
          the
          > new page, but I want to set the index with the XML file and not the HTML
          file.
          >
          > The following line works if I set the default_index variable through
          > FlashVars, but doesn't read it if the default_index is set in the XML.
          >
          > comboBox.selectedIndex=default_index
          >
          > Any ideas?
          >
          > Here's the code that populates the comboBox and sets the default index:
          >
          > var myData:XML = new XML ();
          > myData.ignoreWhite = true;
          > myData.load ("myData.xml");
          > myData.onLoad = function (success:Boolean):Void
          > {
          > if (success)
          > {
          > var data:XMLNode = this.firstChild;
          > var children:Array = data.childNodes;
          > var numItems:Number = children.length;
          >
          > for (var i:Number = 0; i < numItems; i++)
          > {
          > var child:XMLNode = children;
          > var name:String = child.attributes.name;
          > var URL:String = child.attributes.URL;
          > comboBox.addItem (name,URL);
          > }
          > }comboBox.selectedIndex=default_indx
          > }
          >
          > Thanks in advance!
          >


          • 2. Re: Setting selectedIndex via XML
            catcherintherye Level 1
            Thanks.

            That now enables me to set the selectedIndex in the XML file, but I want to be able to set the selected index for each property on each property line.

            For example, I would like to use an XML line like this:

            <datanode>
            <villa name="Villa 2" URL=" http://www.URL2.com" default_index="1"/>
            </datanode>

            rather than this:

            <datanode default_index="1"/>
            <villa name="Villa 2" URL=" http://www.URL2.com" />
            </datanode>

            If the user selects "Villa 1" from the drop down, I want it to take them to a new page that has "Villa 1" as the selected item. The selectedIndex should be determined by whatever the user selects.

            Hope that makes sense! Thanks for your help, and I hope you can give me just a little more guidance!!!
            • 3. Re: Setting selectedIndex via XML
              Level 7
              I think I understand you. If you want to assign the selected index based on
              the url of the movie clip, this might work. It assumes the .SWF is located
              in the same folder as the HTML page.


              <datanode >
              <villa name="Villa 1" URL=" http://www.URL1.com" default_index="0"//>
              <villa name="Villa 2" URL=" http://www.URL2.com" default_index="1"//>
              <villa name="Villa 3" URL=" http://www.URL3.com" default_index="2"/ />
              </datanode>


              import mx.xpath.XPathAPI;

              var myData:XML = new XML ();
              myData.ignoreWhite = true;
              myData.load ("myData.xml");
              myData.onLoad = function (success:Boolean):Void
              {
              if (success)
              {
              var data:XMLNode = this.firstChild;
              var children:Array = data.childNodes;
              var numItems:Number = children.length;

              //populate the combo box

              // get the folder path of the movie
              var url = _level0._url;
              var urlPath = url.substring(0,url.lastIndexOf("/"))

              // use the XPathAPI to find the
              var node = XPathAPI.selectSingleNode(data, "*/villa[@URL='" + urlPath
              +"']");
              if (!node){
              // couldn't find the node -- either doesn't exist or this code doesn't
              work
              //-- or perhaps there is a case-sensitive thing happening
              }
              else{
              var defIdx = Number(node.attributes["default_index"]);
              if(!isNaN(defIdx) ){comboBox..selectedIndex = defIdx;}
              }

              }

              }
              }



              "catcherintherye" <webforumsuser@macromedia.com> wrote in message
              news:e9oj7f$jj$1@forums.macromedia.com...
              > Thanks.
              >
              > That now enables me to set the selectedIndex in the XML file, but I want
              to be
              > able to set the selected index for each property on each property line.
              >
              > For example, I would like to use an XML line like this:
              >
              > <datanode>
              > <villa name="Villa 2" URL=" http://www.URL2.com" default_index="1"/>
              > </datanode>
              >
              > rather than this:
              >
              > <datanode default_index="1"/>
              > <villa name="Villa 2" URL=" http://www.URL2.com" />
              > </datanode>
              >
              > If the user selects "Villa 1" from the drop down, I want it to take them
              to a
              > new page that has "Villa 1" as the selected item. The selectedIndex should
              be
              > determined by whatever the user selects.
              >
              > Hope that makes sense! Thanks for your help, and I hope you can give me
              just a
              > little more guidance!!!
              >
              >


              • 4. Re: Setting selectedIndex via XML
                catcherintherye Level 1
                Thanks again!

                Yes, I'd like to assign the selected index based on the url of the movie clip.

                The SWF file is in a different folder than the HTML:

                SWF is in:
                www.mysite.com/FolderA/SWF.swf

                HTML is in:
                www.mysite.com/Folder1/Folder2/Folder3/Folder4/Folder5/HTML.html

                Can you adjust the code you gave me to take this into account? As long as it's not too much trouble - You've been a big help already!

                • 6. Re: Setting selectedIndex via XML
                  Level 7

                  <datanode >
                  <villa name="Villa 1" URL=" http://www.URL1.com" default_index="0"//>
                  <villa name="Villa 2" URL=" http://www.URL2.com" default_index="1"//>
                  <villa name="Villa 3" URL=" http://www.URL3.com" default_index="2"/ />
                  </datanode>


                  import mx.xpath.XPathAPI;

                  // some utility functions
                  function removeUrlScheme(s){
                  var delimiter:String = "://";
                  var dPos = s.indexOf(delimiter);
                  if (dPos > -1){return s.substr(dPos + delimiter.length);}
                  return s;
                  }
                  function removeUrlQueryString(s){
                  var qPos = s.indexOf("?");
                  var result= s;
                  if (qPos > -1){result= result.substr(0,qPos);}
                  return result;
                  }
                  function baseUrlPath(s){
                  var result = removeUrlScheme(removeUrlQueryString(s));
                  var fragments = result.split("/");
                  result = fragments[0]=="" ? fragments[1] : fragments[0];
                  return result;
                  }

                  //
                  var myData:XML = new XML ();
                  myData.ignoreWhite = true;
                  myData.load ("myData.xml");
                  myData.onLoad = function (success:Boolean):Void
                  {
                  if (success)
                  {
                  var data:XMLNode = this.firstChild;
                  var children:Array = data.childNodes;
                  var numItems:Number = children.length;

                  //populate the combo box

                  // get the basepath of the movie
                  var bPath = baseUrlPath(_level0._url);
                  // use the XPathAPI to find the
                  var xpath = "*/villa[@URL='" + bPath +"']");
                  var node = XPathAPI.selectSingleNode(data, xpath);

                  if (!node)
                  {
                  // couldn't find the node
                  // could be a case issue -- try with lowercase xml copy
                  var lcXml = new XML();
                  lcXml.parseXML(data.toString().toLowerCase());
                  var lcData = lcXml.firstChild;
                  node = XPathAPI.selectSingleNode(lcData, xpath.toLowerCase());
                  }

                  if (node)
                  {
                  var defIdx = Number(node.attributes["default_index"]);
                  if(!isNaN(defIdx) ){comboBox..selectedIndex = defIdx;}
                  }


                  }
                  }


                  "catcherintherye" <webforumsuser@macromedia.com> wrote in message
                  news:e9oooj$7k2$1@forums.macromedia.com...
                  > Thanks again!
                  >
                  > Yes, I'd like to assign the selected index based on the url of the movie
                  clip.
                  >
                  > The SWF file is in a different folder than the HTML:
                  >
                  > SWF is in:
                  > www.mysite.com/FolderA/SWF.swf
                  >
                  > HTML is in:
                  > www.mysite.com/Folder1/Folder2/Folder3/Folder4/Folder5/HTML.html
                  >
                  > Can you adjust the code you gave me to take this into account? As long as
                  it's
                  > not too much trouble - You've been a big help already!
                  >
                  >
                  >


                  • 7. Re: Setting selectedIndex via XML
                    catcherintherye Level 1
                    Got it all in - But am getting the following syntax error:

                    **Error** Scene=Scene 1, layer=Drop Menu, frame=1:Line 50: Syntax error.
                    var xpath = "*/villa[@URL='" + bPath +"']");
                    • 8. Re: Setting selectedIndex via XML
                      Level 7

                      var xpath = "*/villa[@URL='" + bPath +"']";


                      "catcherintherye" <webforumsuser@macromedia.com> wrote in message
                      news:e9qu75$rm7$1@forums.macromedia.com...
                      > Got it all in - But am getting the following syntax error:
                      >
                      > **Error** Scene=Scene 1, layer=Drop Menu, frame=1:Line 50: Syntax error.
                      > var xpath = "*/villa[@URL='" + bPath +"']");