views:

34

answers:

2

Is there a way to modify xml document via actionscript? I have bare xml file i.e.

<words>
    <word>
    <name>this</name>
    <title>that</title>
    </word>
</words>

I want to add more words on this file via actionscript. Is that possible? If yes, what tag should I look into?

Thanks, Rex

A: 

You can use E4X in ActionScript 3 to modify an XML object.

Here is a great guide to using XML in AS3 Kirupa.com - Using XML in Flash CS3/AS3.

First you will need to load the XML document into your SWF, then you will be able to modify the data. To save it back out, you will need to use a serverside script of some sort as shown here: actionscript.org - Sending XML data from AS3 to PHP.

TandemAdam
Thank you for tell me the technique... now I know what needs to be done.. I was trying to modify the external file with actionscript directly... thanks again..
rex
+2  A: 
var ldr:URLLoader = new URLLoader();
ldr.addEventListener(Event.COMPLETE, onLoad);
ldr.load(new URLRequest("file.xml"));//or any script that returns xml

function onLoad(e:Event):void
{
  var loadedText:String = URLLoader(e.target).data;
  trace(loadedText);

  //create xml from the received string
  var xml:XML = new XML(loadedText);
  trace(xml.toXMLString());

  //modify it
  var word2:XML = <word/>;
  var name:XML = <name/>;
  name.appendChild("the name");
  word2.appendChild(name);
  var title:String = "asdasd";
  word2.appendChild("<title>" + title + "</title>");
  xml.appendChild(word2);
  trace(xml.toXMLString());

  //save it
  var ldr:URLLoader = new URLLoader();
  ldr.addEventListener(Event.COMPLETE, onSave);
  //listen for other events here.
  var data:URLVariables = new URLVariables();
  data.xml = xml.toXMLString();
  var req:URLRequest = new URLRequest("savexml.php");
  req.data = data;
  ldr.load(req);
}

function onSave(e:Event):void
{
  trace("save success");
}

Check out URLLoader and URLRequest

Amarghosh
thank you taking you time to writ the code... makes it lot easy to understand..
rex