<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0">
  <channel>
    <title>SnippetRepoBrowser</title>
    <link>http://snippet.gnstudio.com/snippetrepobrowser/index</link>    
    <description>RSS Feed of SnippetRepoBrowser (Global RSS)</description>    
    <language>en-us</language>
    <pubDate>Fri, 18 May 2012 14:27:45 +0200</pubDate>
    <docs>SnippetRepoBrowser/index</docs>
    <generator>SnippetRepoBrowser Feed Generator</generator>
    <item>
      <title><![CDATA[Escape special characters contained in the attribute of an XML stream]]></title>
      <link>http://snippet.gnstudio.com/viewtopic/88</link>
      <description><![CDATA[Regular Expressions are very powerful but sometime not so easy to use, recently we have had the need to get a deserialize an object that cames from a database and that contains in each node the name of the property, its value and its datatype. <br />The raw binary data that we got are expressed with the following syntax <br /><span style="font-family:courier new"><br />&lt;property name=&quot;crop&quot; value=&quot;&quot; type=&quot;com.gnstudio.rikorda.core.model.canvas.vo::Crop&quot; &gt;<br />&lt;property name=&quot;points&quot; value=&quot;&quot; type=&quot;__AS3__.vec::Vector.&lt;flash.geom::Point&gt;&quot; &gt;<br />&lt;property type=&quot;__AS3__.vec::Vector.&lt;flash.geom::Point&gt;&quot; &gt;<br />&lt;property name=&quot;point&quot; value = &quot;&quot; type = &quot;flash.geom::Point&quot;&gt;<br />&lt;property name=&quot;x&quot; value=&quot;&quot; type=&quot;Number&quot; /&gt;<br />&lt;property name=&quot;y&quot; value=&quot;&quot; type=&quot;Number&quot; /&gt;&lt;/property&gt;&lt;/property&gt; </span><br /><br /><br />In the Flash Platform when you try to read this UTF bytes and cast them to an XML object you get an exception because there is the <span style="font-weight:bold">&lt;</span> and the <span style="font-weight:bold">&gt;</span> chars in the attribute, in order to solve this issue we used a regular expression to get the value stored in the attribute. The regular expression uses a named group to access this data into the exec method and inside this method there is the logic to replace the <span style="font-weight:bold">&lt;</span> and <span style="font-weight:bold">&gt;</span> chars with the right HTML entities. <br /><br />Some useful links: <br /><a href="http://gnosis.cx/publish/programming/regular_expressions.html" target="_blank">http://gnosis.cx/publish/programming/regular_expressions.html</a> <br /><a href="http://www.radsoftware.com.au/articles/regexlearnsyntax.aspx" target="_blank">http://www.radsoftware.com.au/articles/regexlearnsyntax.aspx</a> <br /><a href="http://www.radsoftware.com.au/regexdesigner/" target="_blank">http://www.radsoftware.com.au/regexdesigner/</a> <br /><a href="http://help.adobe.com/en_US/ActionScript/3.0_ProgrammingAS3/WS5b3ccc516d4fbf351e63e3d118a9b90204-7e9a.html" target="_blank">http://help.adobe.com/en_US/ActionScript/3.0_...3e3d118a9b90204-7e9a.html</a> <br /><a href="http://livedocs.adobe.com/flex/3/html/help.html?content=12_Using_Regular_Expressions_09.html" target="_blank">http://livedocs.adobe.com/flex/3/html/help.ht...gular_Expressions_09.html</a> <br /><a href="http://www.regular-expressions.info/examples.html" target="_blank">http://www.regular-expressions.info/examples.html</a> <br /><a href="http://www.regular-expressions.info/brackets.html" target="_blank">http://www.regular-expressions.info/brackets.html</a> <br /><a href="http://wiki.tcl.tk/989" target="_blank">http://wiki.tcl.tk/989</a><br />]]><![CDATA[<div class="divcode"><pre lang="actionscript">var content:String = &#39;&lt;property name=&quot;crop&quot; value=&quot;&quot; type=&quot;com.gnstudio.rikorda.core.model.canvas.vo::Crop&quot; &gt;&#39; +<br />		  	&#39;&lt;property name=&quot;points&quot; value=&quot;&quot; type=&quot;__AS3__.vec::Vector.&lt;flash.geom::Point&gt;&quot; &gt;&#39; +<br />			&#39;&lt;property type=&quot;__AS3__.vec::Vector.&lt;flash.geom::Point&gt;&quot; &gt;&#39; +<br />			&#39;&lt;property type=&quot;__AS3__.vec::Vector.&lt;flash.geom::Point&gt;&quot; &gt;&#39; +<br />		  			&#39;&lt;property name=&quot;point&quot; value = &quot;&quot; type = &quot;flash.geom::Point&quot;&gt;&#39; +<br />		  					&#39;&lt;property name=&quot;x&quot; value=&quot;&quot; type=&quot;Number&quot; /&gt;&#39; +<br />		  					&#39;&lt;property name=&quot;y&quot; value=&quot;&quot; type=&quot;Number&quot; /&gt;&#39; +<br />		  			&#39;&lt;/property&gt;&#39; +<br />		  	&#39;&lt;/property&gt;&#39;;<br />			<br />			<br /> trace(&quot;CONTENT BEFORE:&quot;, content, &quot;\n&quot;);<br /><br />var ltExp:RegExp = new RegExp(&quot;\\&lt;&quot;, &quot;g&quot;);<br />var gtExp:RegExp = new RegExp(&quot;\\&gt;&quot;, &quot;g&quot;);<br /><br />var attributeName:String = &quot;type&quot;;<br />var groupName:String = &quot;attribute&quot;;<br /><br />var attributes:RegExp = new RegExp(&quot;&lt;(?:[^&gt;\&quot;&#39;]|\&quot;[^\&quot;]*\&quot;|&#39;[^&#39;]*&#39;)+?\\s&quot; + attributeName + &quot;\\s*=\\s*(?P&lt;&quot; + groupName + &quot;&gt;\&quot;[^\&quot;]*\&quot;|&#39;[^&#39;]*&#39;)(?:[^&gt;\&quot;&#39;]|\&quot;[^\&quot;]*\&quot;|&#39;[^&#39;]*&#39;)*&gt;&quot;, &quot;g&quot;);<br /><br />var err:Error;<br />do { <br />	<br />	try{<br />		<br />		var data:String = attributes.exec(content)[groupName];<br />		var clone:String = data;<br />		<br />		var toReplace:Boolean;<br />		<br />		if(clone.search(gtExp) &gt; -1){<br />			<br />			clone = clone.replace(gtExp, &quot;&gt;&quot;);<br />			toReplace = true;<br />			<br />		}<br />		<br />		if(clone.search(ltExp) &gt; -1){<br />			<br />			clone = clone.replace(ltExp, &quot;&lt;&quot;);<br />			toReplace = true;<br />			<br />		}<br />		<br />		if(toReplace){<br />			<br />			var reg:RegExp = new RegExp(&quot;\\&quot; + data, &quot;g&quot;);<br />			content = content.replace(reg, clone);<br />						<br />		}		<br />	<br />	}catch(error:Error){<br />		<br />		err = error<br />		break;<br />		<br />	}<br />	<br />	<br /> } while ( !err);<br /> <br /> trace(&quot;CONTENT AFTER:&quot;, content);<br /> </pre></div><br /><br />]]></description>
      <author>GiorgioNatili</author>
      <pubDate>Wed, 21 Apr 2010 04:39:39 +0200</pubDate>
      <category>ActionScript</category>
      <guid>http://snippet.gnstudio.com/viewtopic/88</guid>
   </item>
  </channel>
</rss>
