AS3 Replace Special XML Characters with Entity Names (Partial Set)


/ Published in: ActionScript 3
Save to your folder(s)

These five characters should be replaced with their HTML entity names before being used in XML, otherwise they may cause the XML to be invalid.


Copy this code and paste it in your HTML
  1. // http://xml.silmaril.ie/authors/specials/
  2.  
  3. function replaceSpecialXmlChars($str:String):String {
  4. var regExp:RegExp;
  5. regExp = /&/g;
  6. $str = $str.replace(regExp, "&");
  7. regExp = /</g;
  8. $str = $str.replace(regExp, "&lt;");
  9. regExp = />/g;
  10. $str = $str.replace(regExp, "&gt;");
  11. regExp = /"/g;
  12. $str = $str.replace(regExp, "&quot;");
  13. regExp = /'/g;
  14. $str = $str.replace(regExp, "&apos;");
  15. return $str;
  16. }
  17.  
  18. var myStr:String = "<&>\"'<&>\"'";
  19. trace(myStr);
  20. trace(replaceSpecialXmlChars(myStr));
  21.  
  22. // OUTPUT
  23. // <&>"'
  24. // &lt;&amp;&gt;&quot;&apos;

URL: http://xml.silmaril.ie/authors/specials/

Report this snippet


Comments

RSS Icon Subscribe to comments

You need to login to post a comment.