8 Replies Latest reply on Jul 19, 2007 12:32 PM by toddmz2

    Add XML attribute

    toddmz2 Level 1
      Right now I'm working with a very simple .xml file that pulls in data ultimately resulting in the generation of run-time components. Here is a simplified version:

      <data>
      <item type="Button" x="50" y="100" label="Button 1" />
      <item type="Button" x="50" y="300" label="Button 2" />
      </data>

      I figured out how to delete an attribute, such as 'label' with this:

      if( myXML.item[ 0 ].attributes()[ i ].name()== "label')
      {
      delete myXML.build.item[ 0 ].attributes()[ i ];
      i--;
      }

      Now, the question is how do I add another attribute -- such as 'height'? Any help would be greatly appreciated. Thanks.
        • 1. Re: Add XML attribute
          toddmz2 Level 1
          Correction - the line to delete the attribute shouldn't include the 'build' node. Either way, I've got that figured out. Now, I need to figure out how to add an attribute.
          • 2. Re: Add XML attribute
            ntsiii Level 3
            It is unbelievably simple:
            myNode.@myAttribute = "myValue";

            Tracy
            • 3. Re: Add XML attribute
              toddmz2 Level 1
              I'm sorry, I should have explained my situation better. That's exactly the result I'm looking for, but I need to be able to set it dynamically and I'd like not to have to use a massive switch statement to switch between the new attributes. Example.

              function setAttribute( attributeName:String ):void
              {
              myNode.@attributeName = "myValue";
              }

              Problem is '@attributeName' is taken literally where I need more of a 'this[ attributeName ]' = "myValue";
              • 4. Re: Add XML attribute
                ntsiii Level 3
                Ok:
                myNode.attribute("attributeName") = "myValue";

                Tracy
                • 5. Re: Add XML attribute
                  toddmz2 Level 1
                  I tried that and I don't know if I'm not setting it up correctly or what, but I'm getting the following error:

                  1105: Target of assignment must be a reference value.

                  This is how I'm setting it up:

                  myXML.item[ itemIndex ].attribute( "attributeName" ) = "myValue";

                  Any clue on what I'm doing wrong? Thanks.
                  • 6. Re: Add XML attribute
                    ntsiii Level 3
                    That looks ok.

                    Break it up into steps so you can make sure you have valid references at each point.
                    Tracy
                    • 7. Re: Add XML attribute
                      toddmz2 Level 1
                      I must fundamentally be doing something wrong. I've ran through a dozen or so tests and even with a simple test, I can't get it to work:

                      var x1:XML = new XML( '<item type="test" />' );
                      x1.@name = "test 1"; //This works
                      x1.attribute( "name" ) = "test 2"; //This produces an error with or without the line above present.

                      I really don't want the second line (the one with @name). I want to be able to dynamically add run-time attributes. Any ideas? Thanks.
                      • 8. Add XML attribute
                        toddmz2 Level 1
                        Ok, I just stumbbled upon the solution:

                        x1.@[ "name" ] = "test 2";

                        That appears to do it. I don't know if the other way just doesn't work or if I was simply doing it wrong. Thanks for all your help.