How can I dynamically edit XML node values ​​in ActionScript?

/* 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>;
+3
source share
5 answers

This is similar to what you want. This is just your code with fixed typos.

        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);
+4
source

I could not edit the elements inside the XML object after creating the object, and the Adobe documentation is not clear if this is possible.

To set values ​​dynamically, I created a temporary line and added all my XML nodes and attributes here. Then you can simply create an xml object by specifying your temporary string as a single parameter.

Sort of:

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 will get

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

, , XML . , XML, toString() . , , XML !

+2

I just confirmed 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'
}
+1
source

If you only have 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;
 }
+1
source

Using E4X Syntax in ActionScript 3 I assume it will be something like:

report.prop5[0] = false;
-2
source

Source: https://habr.com/ru/post/1699665/


All Articles