6 Replies Latest reply on Oct 16, 2006 2:01 AM by jpwrunyan

    Attribute names

    KostyaS
      For example:

      var xmlData = <item at1="val1" two="2" 3="three" />

      Can I get attribute names list ? Please post example code in answer.

      TIA
        • 1. Re: Attribute names
          ntsiii Level 3
          The snippet below takes an xml node(nodeCur), loops over the attributes list and builds an array that contains the attribute name and value for each attribute. It comes from a sample app that allows you to edit an xml file.
          Sorry that the forum will remove the formatting

          var aDPAttributes:Array = new Array();
          var xlAttributes:XMLList = nodeCur.@*;
          var attribute:Attribute;
          for ( var i:int = 0; i < xlAttributes.length(); i++) {
          aDPAttributes.push({name:xlAttributes.name(),value:xlAttributes});
          }
          dgAttributes.dataProvider = aDPAttributes; //set the property sheet dataProvider

          Tracy
          • 2. Re: Attribute names
            ntsiii Level 3
            That code snippet is BAD! Here is the corrected code:

            var aDPAttributes:Array = new Array();
            var xlAttributes:XMLList = nodeCur.@*;
            for ( var i:int = 0; i < xlAttributes.length(); i++) {
            aDPAttributes.push({name:xlAttributes.name(),value:xlAttributes});
            }
            dgAttributes.dataProvider = aDPAttributes;

            That will make more sense. I think the other was when I was in transition from using a for-in loop

            Tracy
            • 3. Re: Attribute names
              jpwrunyan Level 1
              I have been trying to figure out how to do the same thing (get the attribute NAMES out of XML... not just the values).

              However, the above code snippet still seems wrong... there is no name() method and you aren't using "i" anywhere...
              • 4. Re: Attribute names
                Matlus Level 1
                Tracy has gone through this before so I'll try and help...

                Basically, an element of an XMLList can be treated like an instance of an XML object. Take a look at the live docs for help on the XML class and you'll see that it has a method called name(); If you look at the documentation for XMLList you'll find my comments at the very bottom (since I was stumped as well until Tracy helped).

                var xmlNode:XML = <item at1="val1" two="2" 3="three" />;
                var attributes:XMLList = xmlNode.attributes();
                for (var i:int; i < attributes.length(); i++) {
                trace("Name: " + attributes of i.name() + " Value: " + attributes of i);
                }

                NOTE: because the forum software eats up square brackets, the words "attributes of i" should be replaced with the normal array syntax.

                • 5. Re: Attribute names
                  ntsiii Level 3
                  I have discovered that the forum software will leave the array brackets if you put a space between them and the index:
                  aDPAttributes.push({name:xlAttributes[ i ].name(),value:xlAttributes[ i ]});

                  Tracy
                  • 6. Re: Attribute names
                    jpwrunyan Level 1
                    Thanks,
                    I did figure out the name() thing eventually, not sure if maybe I was looking at the wrong API (I think name() is listed in XML but not XMLList).
                    I also figured out the brackets i problem, too. I appreciate the code sample though, it's very easy to understand the syntax (easier than the API's explanation anyway).