Hi,

I have a string given to me by a user. I want to create XML so I can send the 
string to the server as the value of an XML node. E.g. like this:
<value>string</value>.

However, if I do it like this:

new XML("<value>" + string + "</value>");

that works great until somebody gives me a string e.g.:

var string:String = "a <foo>string</foo>/&";

Given string above, how do I construct an XML node with string representation 
of 
<value>a &lt;foo&gt;string&lt;/foo&gt;/&amp;</value>
?

I'm missing an xmlEscape() function so that I can do:
new XML("<value>" + xmlEscape(string) + "</value>");

Or how else do I create an XML object where the special chars are encoded as 
XML character entity references?

I can pretty easily construct it myself from either regular expressions or 
split/join, but I just find it very hard to believe there isn't a pre-existing 
function for this. Whenever I find myself escaping or encoding by hand, I 
pretty much know I'm going the wrong direction, and here-be-dragons. 

E.g. http://tech.groups.yahoo.com/group/flexcoders/message/84143
Has an incomplete (== buggy) code snippet. In that thread, htmlDecode() doesn't 
decode &pound; which is a valid HTML character reference (but not a valid XML 
character reference). My point is that if we all start writing home-grown 
encoding/decoding, bugs will happen.

Is it really true that Flex doesn't provide this out of the box? Am I 
misunderstanding something? 

Thank you for reading this far!

Peter

P.S.: Please don't tell me about escape() because it URL-encodes, which is not 
the same thing. (For "a&b" I want "a&amp;b", not "a%26b).

Reply via email to