views:

2971

answers:

5
/* I start with this: */

<Report>
    <prop1>4</prop1> 
    <prop2>2255</prop2> 
    <prop3>true</prop3> 
    <prop4>false</prop4> 
    <prop5>true</prop5> 
</Report>

/* I want this result (change the value of node "prop5"): */

<Report>
    <prop1>4</prop1> 
    <prop2>2255</prop2> 
    <prop3>true</prop3> 
    <prop4>false</prop4> 
    <prop5>false</prop5> 
</Report>

/* I tried this: */

var reportXML:XML = 
    <Report>
        <prop1>4</prop1> 
        <prop2>2255</prop2> 
        <prop3>true</prop3> 
        <prop4>false</prop4> 
        <prop5>true</prop5> 
    </Report>;

var myArray:Array = [{xmlNodeName: "prop5", value: false}];

for each (var item:Object in myArray)
{
    report.xml[item.xmlNodeName] = item.value.toString();
}

/* But this just adds a new node, resulting in this: */

<Report>
    <prop1>4</prop1> 
    <prop2>2255</prop2> 
    <prop3>true</prop3> 
    <prop4>false</prop4> 
    <prop5>true</prop5> 
    <prop5>false</prop5> 
</Report>;
A: 

Using E4X syntax in ActionScript 3 I guess that would be something like:

report.prop5[0] = false;
Christophe Herreman
This didn't work either. This created a new node with a different namespace (the namespace was based on the name of the class I was in).
Eric Belair
A: 

I just verified that this works:

private var reportXML:XML = 
    <Report>
        <prop1>4</prop1>
        <prop2>2255</prop2>
        <prop3>true</prop3>
        <prop4>false</prop4>
        <prop5>true</prop5>
    </Report>;

private function changeXML():void {
    reportXML.prop5[0] = 'false';
    trace(reportXML.prop5);  // traces 'false'
}
defmeta
This still does not answer my original question. Look at my original example - I want this to be DYNAMIC. I have an Array of Objects that specify the nodes to be updated. My whole purpose in posting this is to find a way to not have to hardcode "prop5" into my application.
Eric Belair
+1  A: 

This seems to be doing exactly what you want. It's just your code with some typos fixed.

  var reportXML:XML = 
   <Report>
    <prop1>4</prop1> 
    <prop2>2255</prop2> 
    <prop3>true</prop3> 
    <prop4>false</prop4> 
    <prop5>true</prop5> 
   </Report>;

  var myArray:Array = [{xmlNodeName: "prop5", value: false}];

  for each (var item:Object in myArray)
  {
   reportXML[item.xmlNodeName] = item.value.toString();
  }

  trace(reportXML);
Jesse Millikan
I'll give you credit for this one.... I've got it working now. Both in pseudo code and real code. Thanks.
Eric Belair
A: 

Eric did you ever get a solution to this problem? I have the exact same problem, i create an xml (its in 1 namespace), then i get some data from another function and i want to edit this xml's nodes but it just adds other nodes with wrong namespace.

MartinD
A: 

if you only have the node you can go like this

var node:XML
inp = new textfield(style, node.text());
inp.addEventListener(TextEvent.TEXT_INPUT, change, false, 0, true);
addChild(inp);

private function change(e:TextEvent):void
 {
 XML(node.parent())[node.name()][node.childIndex()] = inp.text+e.text;
 }
manu