0
votes

I'm trying to update a value in a subnode of an XML structure. New value should come from an Input TextField whose instance name is the same of the value of the attribute of the node I should update.

For example, when focusing out textfield called "label10", I should edit the node:

<label id="label10">
    <eng>Description</eng>
    <de>DE Description</de>
</label>

...and that's ok, I achieved it using this callback function:

private function onFocusOut (evt:FocusEvent):void {
        var nodeToModify:XML = xmlFillData.label.(@id==evt.target.name)[0];
        trace ("nodeToModify is " +nodeToModify );
}

Now my problem is I wish to replace the content of the subnode eng or de, depending on a variable defined in a static class called VarHolder, but I'm not able to do it.

I've tried to use this line inside the callback function:

 nodeToModify.replace (VarHolder.activeLang , evt.target.text);

but then if I trace nodeToModify, this is the result ( tag disappeared)

<label id="label10">
rrr
<de>DE Description</de>

Any help?


EDIT (and solved): trying to implement @jens answer. This is how I did it

 nodeToModify.replace (VarHolder.activeLang,  new XML("<" + VarHolder.activeLang + ">" +  evt.target.text + "</" + VarHolder.activeLang + ">"));
1

1 Answers

2
votes

XML.replace() does expect the 2nd parameter to be an XML-Object which then is converted to a (XML-)string. Meaning: it replaces '<eng>Description</eng>' with 'rrr' and its working as intended. You need to pass it an XML-object or pass it '<eng>rrr</eng>'. If the value of VarHolder.activeLang is plainly 'eng', you could use this:

nodeToModify.replace (VarHolder.activeLang ,  "<" + VarHolder.activeLang + ">" + 
                           evt.target.text + "</" + VarHolder.activeLang + ">");

See this link for more information on XML.replace(): http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/XML.html#replace%28%29