How can I dynamically edit XML node values in ActionScript?

15,169

Solution 1

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

Solution 2

I've not been able to edit elements within an XML object after the object has been created and the Adobe documentation isn't clear on if this is even possible.

For dynamically setting values, I created a temporary string and appended all my XML Nodes and Attributes in here. Then you can simply create the xml object specifying your temp string as the lone parameter.

Something like:

var tempString:String = "<XML_PARENT><SOME_SUB_NODE>";
tempString += "<SOMETHING_ELSE value=\"" + someTextField.text + "\"/>";
tempString += "</SOME_SUB_NODE></XML_PARENT>";

var xmlObj:XML = new XML( tempString );

Now if you trace xmlObj, you'll get

<XML_PARENT>
    <SOME_SUB_NODE>
        <SOMETHING_ELSE value=""/>
    </SOME_SUB_NODE>
<XML_PARENT>

This will let you dynamically assign whatever you want to the string and then build the XML after the fact. It's not exactly helpful if you then want to edit an existing XML object, but you could just use toString() and modify the string accordingly. It might at least help in the to get started with dynamically building XML files!

Solution 3

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'
}

Solution 4

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;
 }
Share:
15,169
Eric Belair
Author by

Eric Belair

Flex/ActionScript/ColdFusion/SQL programmer.

Updated on June 04, 2022

Comments

  • Eric Belair
    Eric Belair almost 2 years
    /* 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>;