简体   繁体   中英

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

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

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!

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

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

在ActionScript 3中使用E4X语法我想这会是这样的:

report.prop5[0] = false;

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM