0

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();
        }
Papa De Beau
  • 3,744
  • 18
  • 79
  • 137

1 Answers1

1

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>
fsbmain
  • 5,267
  • 2
  • 16
  • 23
  • BAM! Exactly it! Do you recommend i change the question to help those who are searching for this same solution? Or should I keep it as is so they might search for what I was and find your answer? – Papa De Beau Nov 25 '13 at 08:33
  • I have one more question related to this. I made an edit in the top question. – Papa De Beau Nov 25 '13 at 08:45
  • I added it as a full question here. Its a tricky one. http://stackoverflow.com/questions/20201099/as3-urlrequest-and-file-stream-saving-xml-data-error-3013-file-or-directory – Papa De Beau Nov 25 '13 at 20:29