tags:

views:

870

answers:

2

Hello everybody,

I have a strange problem, I have a XMLList with elements who have the attribute position from 0 to x. When I want to add text to a element of that XMLList by the following statement, I get a error message:

textElements.(@position == columnIndex) = "anyString";

1050: Cannot assign to a non-reference value.

What is wrong here?

Thanks Markus

+1  A: 

you have to be a little more specific when dealing with XMLLists. You actually have to tell it which item in the list you are wanting to change, even when you filter it down to only 1. This should do it for you:

textElements.(@position==columnIndex)[0]= "anyString";
invertedSpear
+1  A: 

The use of [0] in the answer by invertedSpear is not actually referencing the XMLList index, it is indicating the position of the child to access of the selected node. For comparison

textElements.(@position==columnIndex).appendChild("anyString");

This will do the same thing as invertedSpear's answer, but will add a child to the selected node, rather than accessing the child node directly by index. A node, if it exists, will always have the child indexed at 0 available, in this case it refers to the value of the node itself.

Consider the following:

var xml:XML = <root><node position="1" /><node position="2" /><node position="3" ><subnode>Test1</subnode><subnode>Test2</subnode></node></root>;

var c:XMLList = xml.children();
trace(c);

c.(@position == 1)[0] = "first test";
c.(@position == 2).appendChild("SECOND TEST");
c.(@position == 3)[1] = 'FINAL_test';

trace(c); 
trace(c[0][0]);

The first one will work, as shown. The second one will also work, as shown. The third, however, will fail because the node with position == 3 does not have a child at index 1. Notice that this will work...

c.(@size == 3).children()[1] = 'NEW test';
trace(c);
sberry2A
Thanks a lot! your comments helped me a lot! Thats Great!Cheers Markus
Markus
@Markus It is a custom in SO to up vote the helpful answers (by clicking the up arrow) and accept the answer (checking the green tick mark) that helped you the most.
Amarghosh