Delete XML node with AS3

1.4k views Asked by At

I'd like to delete some XML node with AS3. I've searched on google how to do that, but nothing seems to work.

Here a part of my XML :

<feed xmlns:xxxx="xxxx" xmlns:opensearch="xxx" xmlns="xxxx">
    <title/>
    <updated>date</updated>
    <author>
        <name>xxxxx</name>
    </author>
    <id>urn:tag:api@xxxx,date</id>
</feed>

Here my AS3 :

package {

  import flash.text.TextField;
  import flash.events.*;
  import flash.utils.*;
  import flash.display.*;
  import flash.net.*;

    public class Hello extends Sprite
    {  

    // Attributs
    private var xmlLoader:URLLoader = new URLLoader();

    // Constructor
    public function Hello()
    {
      // Call XML Method
      XMLcall();
    }

    // XMLcall Method
    private function XMLcall():void
    {
      xmlLoader.load(new URLRequest("xxx"));
      // When loaded, add event and launch XMLparser
      xmlLoader.addEventListener(Event.COMPLETE, XMLcreate);
    }

    // XMLCreate Method
    private function XMLcreate(e:Event):void
    {
      // Create var myXML which contains the xml
      var myXML:XML = new XML(e.target.data);

      // Delete node
      delete myXML.author;

      // Save as myxml.xml
      var bytes:ByteArray = new ByteArray();
      var fileRef:FileReference=new FileReference();
      fileRef.save(myXML, "myxml.xml");
    }

    }
}

This code works but the node "Author" is still in my xml...

2

There are 2 answers

0
Mahmoud Badri On BEST ANSWER

you have to consider the name space, replace this line:

delete myXML.author;

with:

var ns:Namespace = myXML.namespace();
delete myXML.ns::author;

and it should work fine

0
Pan On

Try add namespace when delete the node, And this page may help

var ns:Namespace = myXML.namespace("xxxx");
delete myXML.ns::author;