替换AS3 XML子节点的值 [英] Replace xml subnode value in AS3

查看:354
本文介绍了替换AS3 XML子节点的值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想更新XML结构的一个子节点的值。新的价值应该来自一个输入文本字段的实例名称是一样的,我应该更新节点的属性值。

例如,聚焦了文本框被称为label10的时候,我要编辑节点:

 <标签ID =label10>
    < ENG>简介< / ENG>
    <德> DE描述< /德>
< /标签>
 

......,不过没关系,我用这个回调函数来实现它:

 私有函数onfocusout在(EVT:FocusEvent)方法:无效{
        变种nodeToModify:XML = xmlFillData.label(@ ID == evt.target.name)[0];。
        跟踪(nodeToModify是+ nodeToModify);
}
 

现在我的问题是,我希望替换子节点ENG的内容或取消,取决于静态的类名为VarHolder定义的变量,但我没能做到这一点。

我试图用这条线的回调函数里面:

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

但如果我跟​​踪nodeToModify,这是结果(标记消失)

 <标签ID =label10>
存款准备金率
<德> DE描述< /德>
 

任何帮助吗?


编辑(和解决):试图执行@jens答案。 这是我做的

  nodeToModify.replace(VarHolder.activeLang,新的XML(<+ VarHolder.activeLang +>中+ evt.target.text +< /+ VarHolder .activeLang +>中));
 

解决方案

XML.replace()并期望第二个参数是一个XML对象,然后转化到(基于XML)字符串。含义:它取代< ENG>简介< / ENG> RRR ,其工作按预期。你需要传递一个XML对象或传递< ENG>存款准备金率< / ENG> 。如果 VarHolder.activeLang 显然是值ENG,你可以使用这样的:

  nodeToModify.replace(VarHolder.activeLang,<+ VarHolder.activeLang +>中+
                           evt.target.text +&所述; /+ VarHolder.activeLang +>中);
 

查看XML.replace(此链接了解更多信息): 的http://help.adobe.com/en_US/FlashPlatform/reference/actionscript/3/XML.html#replace%28%29

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

解决方案

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

这篇关于替换AS3 XML子节点的值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆