views:

310

answers:

2

Something that should be very easy has been the quest of my day.

How do you set a variable attribute of a xml element?

This is what I expected to work:

xmlElement.attribute(variableAttr) = "the variable attribute is set to this string";

However, I'm getting some error that this value can only be retrieved as a reference and not set.

Ofcourse, the following does not work either as it will look for the attribute named "variableAttr" and not for the attribute named after the value of the variable variableAttr:

xmlElement.@variableAttr = "example";
+3  A: 

You have to enclose your variable name with square bracket @[my var] :

xmlElement.@[variableAttr] = "example";

Patrick
A: 
bhups
No with e4x syntax this will create an xml like this: <attributes><variableAttr>example</variableAttr></attributes>
Patrick
but it'll work if xmlElement is of type XMLNode.. :)
bhups
1 - Can you add your test sample ?2 - You can't reference the variable content using the notation .variableAttr
Patrick
edited the answer with it!
bhups
One more thing to answer the question ;) replace e.attributes.val with e.attributes[val] since in the question val is a variable and not an attribute name. => var name:String="val"; e.attributes[name]="100";
Patrick