Renaming XML Nodes Using E4X in AS3
I have an XML object in AS3 that is formatted like this:
<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 "element") to something more up to date, eg. "nodes" can be "author" and "item" can be "book".
So what would be the best way to do this with E4X in AS3?
0
a source to share
2 answers
you can use setName()
XML object method 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");
}
+7
a source to share
As far as I know, there is no way to do this natively within ActionScript, but here is a simple function that will do just that. First, I convert it to a string, then using a simple regex 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.
0
a source to share