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";

2 Answers 2

9

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

xmlElement.@[variableAttr] = "example";

Sign up to request clarification or add additional context in comments.

Comments

-1

Try

xmlElement.attributes.variableAttr = "example";

example code:var d:XMLDocument = new XMLDocument(); var e:XMLNode; d.appendChild(e = d.createElement("Root")); e.attributes.val = "100"; trace(d.toString()); //prints <Root val="100" />

3 Comments

No with e4x syntax this will create an xml like this: <attributes><variableAttr>example</variableAttr></attributes>
1 - Can you add your test sample ? 2 - You can't reference the variable content using the notation .variableAttr
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";

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.