How to add attribute in XMLLIST without loop in E4X

I have the following xml

var xml:XML = <test> <node id='1'/> <node id='2'/> <node id='3'/> <node id='4'/> <node id='5'/> </test>; var xmlist:XMLList = xml.children(); for each (var node:XML in xmlist) { node.@newAttribute = "1"; } 

I look through each node and add an attribute. How can I do this without loops? I tried this

 xmlist.attributes() .@newAttrib = "1"; 

but I get the error " TypeError: Error # 1089: Assigning to lists with more than one item is not supported ."

+4
source share
2 answers

2 weeks have passed since this question was asked, but there will always be more people in need of help.

TypeError: Error # 1089 caused by the result of more than one object in xml.

I usually accepted this error with the operation = xml.classes. (@id == 1) .students. (@no == 2). @Grade = "A"; The error was caused by the fact that there was more than one student in xml.class, so he tried to return all of them. As the error says: "Assigning to lists with more than one item is not supported." You cannot assign a value to multiple objects at the same time.

And since you add all s to the XMLList, I am not sure of the reason, since I am not using XMLList. it is useless, as I think. Therefore, if you change your code as

 var xml:XML = <test> <node id='1'/> <node id='2'/> <node id='3'/> <node id='4'/> <node id='5'/> </test>; for each (var n:XML in xml) { n.@newAttribute = "1"; } 

The problem must be resolved. But I suggest you use "id" as a unique key. Then you can use this unique key to access certain elements in XML, for example

 xml.node.(@id=="1") .@newAttribute ="1"; 

Hope this helps you. be careful

-Ozan

+1
source

As stated in the error, it is not supported. Since you cannot accomplish this assignment for multiple elements, I see no way to do this without iterating over XML.

For fun, I tried this and got the same error: xml.node.@newAttribute =1

This is just a slightly shorter option:

 var xmlist:XMLList = xml.children(); xmlist.attributes() .@newAttrib = "1"; 
0
source

All Articles