views:

800

answers:

2

I have an XML object in AS3 that is formatted as follows:

<data>
  <nodes>
    <item></item>
    <item></item>
    ...
  </nodes>
  <nodes>
    <item></item>
    <item></item>
    ...
  </nodes>
  ...
</data>

My problem is I want to rename the node names("nodes" and "item") to something more relevant e.g. "nodes" could be "author", and "item" could be "book".

So what would be the best way to do this with E4X in AS3?

A: 

Hi,

As far as I know there is no way to do this natively within ActionScript, however here is a simple function that will accomplish just that. First I am converting it to a string, then using a simple regular expression to find and replace all instances.

function replaceAll(findTag:String, replaceWith:String, source:XML):XML
 {
  var xmls:String = source.toXMLString();
  var findPattern:RegExp = new RegExp(findTag, "g");
  xmls = xmls.replace(findPattern, replaceWith);
  return new XML(xmls);
 }

Hope it helps.

Tyler Egeto
+4  A: 

you can use the setName() method of XML Object in AS3.

example:

   //extract all nodes named "nodes"

    var l:XMLList=data..nodes;
    for each(var n:XML in l){
       n.setName("new_node_name");
    }

    //extract all nodes named "item"

    var l2:XMLList=data..item;
    for each(var n2:XML in l2){
       n2.setName("new_item_name");
    }
OXMO456
I was hoping there would be something like: data..nodes.setName("new_node_name");But that would be asking too much. Thanks!
TandemAdam