as3 xml.replace语法调用

时间:2013-11-25 08:20:50

标签: xml actionscript-3 replace e4x

我需要知道如何使用xml.replace。

这是我的代码:

             myXML.replace("WHAT DO PUT HERE?", <p>testing</p>);
             trace(myXML);

我需要更改choice1和choice2等的标题。 如何使用xml.replace调用它们?

这是XML:

<SETTINGS>
  <Settings Title="choice1">Home</Settings>
  <Settings Title="choice2">Options</Settings>
</SETTINGS>

最后,我如何保存这个新编辑的文件并通过我的函数传递它?

以下功能不起作用。我无法将xml传递给函数和 我认为stream.write期待一个字符串。

public function saveSettings(daFile:XML)
        {
            stream.open(someFile, FileMode.WRITE);
            stream.writeUTFBytes(daFile);
            trace("Settings SAVED!");
            stream.close();
        }

1 个答案:

答案 0 :(得分:1)

我认为,XML.replace在这种情况下不是一个合适的选择,它将取代所有Settings个节点。您可以使用此更具前瞻性的解决方案:

    var xml:XML = <SETTINGS>
          <Settings Title="choice1">Home</Settings>
          <Settings Title="choice2">Options</Settings>
        </SETTINGS>;

    trace("before\n", xml);
    xml.Settings.(@Title == "choice1").* = "Home2";
    xml.Settings.(@Title == "choice2").* = "Options2";

    trace("after\n", xml);

outpout:

before
 <SETTINGS>
  <Settings Title="choice1">Home</Settings>
  <Settings Title="choice2">Options</Settings>
</SETTINGS>
after
 <SETTINGS>
  <Settings Title="choice1">Home2</Settings>
  <Settings Title="choice2">Options2</Settings>
</SETTINGS>