views:

262

answers:

4

I'm having trouble accessing the value of the root node of an XML variable in flex.

For example:

var X:XML=
  <Message Type="abc">
    Content123
  </Message>

I can change the "Type" attribute above with X.@Type="xyz";

But how do I change "Content123" to something else?

If the xml document were longer/deeper, I could say something like X.Entry[11].Cost=2.22; But what do I say in this case? Obviously X="Content456" doesn't work...

+1  A: 

This is one way to do it:

var xml:XML=
  <Message Type="abc">
    Content123
  </Message>;

xml.children()[0] = 'Content456';
fireeyedboy
A: 

If it doesn't have any other child element, you can do X.setChildren("something else"); to achieve this.

Amarghosh
+4  A: 

This is another way, a little safer because you explicitly assign the new value to a text node:

var xml:XML=
  <Message Type="abc">
    Content123
  </Message>;

xml.text()[0] = 'Content456';
Pieter Kuijpers
+1 Good one. Indeed safer than my suggestion.
fireeyedboy
A: 

x.Message would give the value "abc" in your case.. so u can change it easily i guess!

Vennkatesan