简体   繁体   中英

Actionscript 3.0 Updating XML file

Is there a way to modify xml document via actionscript? I have bare xml file ie

<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

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

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 .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM