2 Replies Latest reply on Oct 9, 2009 7:55 AM by JoshDBrown

    Strange behavior when deleting XML nodes

    JoshDBrown

      I am developing a thermocouple product builder for a client.  I have an XML file that looks like this:

       

      <thermocouple>
        <step id="step1">
          <property name="CALIBRATION">
              <option code="K">TYPE-K (CHROMEL-ALUMEL)</option>
              <option code="J">TYPE-J (IRON-CONSTANTAN)</option>
              <option code="N">TYPE-N (NICROSIL-NISIL)</option>
              <option code="T">TYPE-T (COPPER-CONSTANTAN)</option>
              <option code="E">TYPE-E (CHROMEL-CONSTANTAN)</option>
              <option code="S">TYPE-S (PLATINUM-PLAT 10% RH)</option>
              <option code="R">TYPE-R (PLATINUM-PLAT 13% RH)</option>
              <option code="B">TYPE-B (PLAT 6% RH-PLAT 30% RH)</option>
              <option code="C">TYPE-C (TUNG 5% RE-TUNG 26% RE)</option>
          </property>
        </step>
        <step id="step2">
          <property name="MATERIAL">
              <option code="1" ncw="JEC">INCONEL 600</option>
              <option code="2" ncw="NSRBC">304 STAINLESS STEEL</option>
              <option code="3" ncw="NSRBC">316 STAINLESS STEEL</option>
              <option code="4" ncw="NSRBC">310 STAINLESS STEEL</option>
              <option code="5" ncw="NSRBC">446 STAINLESS STEEL</option>
              <option code="6" ncw="JNESRBC">NICROBELL-B</option>
              <option code="7" ncw="JNESRBC">NICROBELL-C</option>
              <option code="8" ncw="JNESRBC">HASTELLOY-X</option>
              <option code="9" ncw="KJNE">TANTALUM</option>
              <option code="22" ncw="JNE">ALUMINA</option>
              <option code="33">MULLITE</option>
              <option code="44" ncw="KJNE">MOLYBDENUM</option>
          </property>

        </step>...etc.

      </thermocouple>

       

      As each step is loaded, an XMLList is culled from the XML file for that step, and then that XMLList acts as the data provider for a combobox.  When the first step has been completed, a flag called "calCode" is set using the @code attribute from the step 1 data.

       

      Steps 2 through 4 have some options that will not work with certain calibration types from step 1.  So I added another attribute to those options, @ncw.  This stands for "not compatible with".  If a calibration code exists inside a certain option's @ncw attribute, that means that option should not be included in the XMLList that is the data provider.  Here's the code I wrote to do this:

       

      myXml = thermoData.lastResult.step.(@id==myStep).property.option ; /* Initialize the XMLList */
                  if(calCode) { /* If the calCode flag is set */
                      for(var i:uint=0;i<myXml.length();i++) { /* For each item in the XMLList */
                          if(myXml.@ncw[i]) { /* If the @ncw attribute exists in the current item */
                              if(myXml.@ncw[i].match(calCode)!=null) { /* If the calCode is found in the value of the @ncw attribute */
                                  delete myXml[i]; /* Delete the current item */
                              }
                          }
                      }    
                  }

       

      Here's where things get screwy. If I choose Type C in the first step, the resulting XMLList for step 2 should be very small because that calibration type is only compatible with a few materials.  It should look like this:

       

      <option code="9" ncw="KJNE">TANTALUM</option>
      <option code="22" ncw="JNE">ALUMINA</option>
      <option code="33">MULLITE</option>
      <option code="44" ncw="KJNE">MOLYBDENUM</option>

       

      However, when I test it, it looks like this:

       

      <option code="2" ncw="NSRBC">304 STAINLESS STEEL</option>
      <option code="4" ncw="NSRBC">310 STAINLESS STEEL</option>
      <option code="6" ncw="JNESRBC">NICROBELL-B</option>
      <option code="8" ncw="JNESRBC">HASTELLOY-X</option>
      <option code="9" ncw="KJNE">TANTALUM</option>
      <option code="22" ncw="JNE">ALUMINA</option>
      <option code="33">MULLITE</option>
      <option code="44" ncw="KJNE">MOLYBDENUM</option>

       

      It seems to be deleting every other item in the XMLList where the calCode exists in the @ncw attribute, instead of every item.  Does anyone know why?  I am only an intermediate ActionScript programmer, and this is my first Flex project ever, so I'm totally willing to accept that I could have made a huge mistake somewhere.  I can post the entire source code if you want to look it over... it's pretty long and I'm rubbish at commenting.  Thanks!

        • 1. Re: Strange behavior when deleting XML nodes
          JoshDBrown Level 1

          I figured it out.  This is probably a pretty amateur mistake.  By checking each node in turn and deleting it if there is a match, I'm unintentionally changing the index number of each subsequent node.  So if myXml[1] gets deleted, all the other nodes have their indices reduced by one, but the loop counter continues to increment, so the node that was 2 is now 1, and it never gets checked.  I think instead of deleting nodes I need to treat the master XMLList as a source and move the non-matching nodes to a duplidate list that will then act as the data provider.

          • 2. Re: Strange behavior when deleting XML nodes
            JoshDBrown Level 1

            Solved!  I did not realize you could do string operations inside an E4X statement.  My complicated for / if loop was reduced to one line:

             

            myXml = thermoData.lastResult.step.(@id==myStep).property[myNum].option.(@ncw.match(calCode)==nul l);