Search code examples
xmlactionscript-3replacee4x

as3 xml.replace syntax calling


I need to know how to use xml.replace.

Here is my code:

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

I need to change the Titles of choice1 and choice2 etc. How do I call them using xml.replace?

Here is the XML:

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

Finally, how would I save this new edited file and pass it thru a function I have?

The function below does not work. I can not pass the xml to the function and the stream.write is expecting a string, I think.

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

Solution

  • I think, XML.replace isn't a proper choice in this case, it will replace all Settings nodes. You can use this more forward solution:

        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>