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();
        }

1 Answer 1

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>
Sign up to request clarification or add additional context in comments.

3 Comments

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?
I have one more question related to this. I made an edit in the top question.
I added it as a full question here. Its a tricky one. stackoverflow.com/questions/20201099/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.