Thursday, July 28, 2011

AS3: Transmute namespace instead of removing it in XML

There are lot many articles and solutions available on web related to the xml namespace. Few guide us over how to handle namespace in xml via as3 while rest have provided solution by removing xmlns tag from the xml.

But I think, there is better way than removing or filtering the namespace. The solution that I use is very clever one. You might have noticed that to put namespace in xml colon character is used. So keeping this property in mind, I have developed a function to replace all the colons from namespaces only with the underscore character.

The code uses regular expression to search the colon character in only namespace. And then replace it with the underscore character.

This way all the namespaces are still available and accessible but in the form of attributes. And this is the advantage of converting it into a attributes.

  * @method transmuter  
  * @author Abhishek Kumar  
 private function transmuter(data:String):String  
  var modified:String = data.replace("xmlns=", "xmlns:default=");  
  var pattern:RegExp = new RegExp(":(?!/)", "gi");  
  var transmuted:String = modified.replace(pattern, "_");  
  return transmuted;  

The function takes xml as a string. So make the xml into string as, xmldata.toString(); then use the function to transmute it. And convert the returned xmldata that is in string format into the xml class format.

No comments:

Post a Comment