0

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?

2 Answers 2

7

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

1 Comment

I was hoping there would be something like: data..nodes.setName("new_node_name"); But that would be asking too much. Thanks!
0

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.

Comments

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.