30 July 2007

Flash 9 (only) XML Class Gotcha

Written by Richard Leggett ( Contact the author of this post )
Published on July 30th, 2007 @ 05:09:26 am, using 265 words, 2509 views
Categories: Flash, Flex, AIR/Apollo

Possible gotcha with the XML class, but I'd like to get this confirmed if anyone has a second to try this out.

It seems that the XML class is converting " to its literal ASCII representation (i.e. the quote mark ") when converting a String into a new XML object. I'm sending this data over XML-RPC and I cannot have quotes being sent as plain-text, they need to be XML/HTML encoded.

Here's a quick test that shows it:

var str:String = "<string>Here is some text, with an image...
&lt;img src=&quot;http://www.google.co.uk/intl/en_uk/images/
logo.gif&quot; /&gt;</string>";

var xml:XML = new XML( str );
trace( xml );

At this point I'd be sending the xml object over the wire with HTTPService. The problem is that trace()/Charles/Fiddler etc is showing that the " is being converted to a real quote (literal) and that can cause a problem with the server which wants encoded copy, and being XMLRPC, I cannot use CDATA in this case. This is not the same behaviour as appears in Flash 8, it also appears that it differs when compiling with the Flex SDK and the Flash CS3 IDE in terms of whether it also turns the < and > entities to their literal representations.

Does anyone know a workaround for this? Can it be expected behaviour? I think I'll have to be sending things as a string internally to avoid the problem for now but it would be good to hear other opinions.

Comments, Pingbacks:

Comment from: tbm
cast with escape?
PermalinkPermalink 30/07/07 @ 05:12
Comment from: Richard Leggett [Member] Email
Afraid not. It's already XML/HTML encoded as it needs to be when reaching the server. It seems that this same code behaves differently in the Flash 9 IDE as it does when running from the Flex SDK strangely.
PermalinkPermalink 30/07/07 @ 06:21
Comment from: paddy
you can try:
trace(xml.toXMLString());

it still doesn't do the quotes though ;(

as mentioned trace(escape(xml)); may be your best bet...
PermalinkPermalink 30/07/07 @ 06:22
Comment from: Richard Leggett [Member] Email
Hi paddy, afraid not, I need it to be HTML encoded (escape will only URL encode).

The only solution I can see right now is to double HTML encode it, so that would be an &amp;quot; for a quote mark. However I still think it's working differently between the compilers, and both are different to Flash 8's behaviour. Still looking into it.
PermalinkPermalink 30/07/07 @ 06:26
Comment from: Ryan Matsikas
var imgSrc:String = "http://www.google.co.uk/intl/en_uk/images/logo.gif";
var imgTag:XML = ;

var xml:XML = Here is some text, with an image { imgTag };
trace( xml );

var ur:URLRequest = new URLRequest('http://myserver.com');
var uv:URLVariables = new URLVariables();
uv.param = xml.toString();

ur.data = uv;
trace(ur.data)
---

Seems to work for me..
PermalinkPermalink 30/07/07 @ 11:15
Comment from: Ryan Matsikas
That was chewed up and spit out by your comment system :\

http://out.chewtinfoil.com/rlXML.txt
PermalinkPermalink 30/07/07 @ 11:18
Comment from: Richard Leggett
Hi Ryan,

Thanks for your sample. Unfortunately I'm not using e4x directly within ActionScript as per your example, the XML is originally dynamically generated as a string (or loaded from elsewhere) and then converted to an XML data type. My posted example probably didn't reflect the situation fully.

So the problem is with the conversion that occurs in the XML constructor/parsing. It doesn't want to preserve the quote entity and there are no flags to allow you to keep it.
PermalinkPermalink 30/07/07 @ 14:28
Comment from: Robert Penner
The E4X is producing valid XML. It's just choosing to encode nothing more than is necessary. If instead, you put the same string in an attribute, the quotes toXMLString() escapes the quote, as necessary. But in that case, it uses a literal greater-than because escaping it isn't necessary. Shouldn't an XML-RPC system be supporting it if it's valid XML data?

I have a somewhat reverse problem. I cannot get toXMLString() to output CDATA in a CDATA block. It outputs the escaped equivalent. So I can't put ActionScript source code into a CDATA and have it be legible coming back out of the XML object. If anyone figures out how to do this, let me know.
PermalinkPermalink 02/08/07 @ 02:21
Comment from: Richard Leggett [Member] Email
Hi Robert,

This is correct... A fully functional XML-RPC server should support unescaped quote marks in text nodes. But I was thrown by the commentary on:

http://www.w3schools.com/xml/xml_cdata.asp

Which suggests it is a good practice to encode those entities. So all in all, I don't have a problem at present, I just wanted the data to be the same as the server generated content which does use &quot;. The CDATA scenario on the other hand definitely looks to be an issue. Do you have a sample online?
PermalinkPermalink 02/08/07 @ 02:42
Comment from: Robert Penner
Re: CDATA, it turns out AS3 handles it fine. I'm encountering the issue in JSFL, which gained E4X in Flash CS3 when we upgraded to the latest SpiderMonkey. I assumed it would work the same as in AS3, since everything else seems to.

Here's a simple test:


var script:String = "trace(1 < 2 && 2 > 1)";
var str:String = '';
var xml:XML = new XML( str );
trace( xml.toXMLString() );

-- AS3 Output --
1)]]>

-- JSFL Output --
trace(1 < 2 && 2 > 1)

hope all the characters make it through ok.
PermalinkPermalink 02/08/07 @ 13:37
Comment from: Robert Penner
Ack, that got mangled from two different directions. The crucial part is that str is:

&lt;script&gt;&lt;![CDATA[trace(1 &lt; 2 && 2 &gt; 1)]]&gt;&lt;/script&gt;
PermalinkPermalink 02/08/07 @ 13:45
Comment from: Robert Penner
Oh well, I tried. The point is that AS3 outputs CDATA as expected, while SpiderMonkey 1.6 (JSFL) escapes it. I wonder what happens in Firefox's current E4X.
PermalinkPermalink 02/08/07 @ 13:49
Comment from: Richard Leggett [Member] Email
Apologies for the blog mangling up the code there. It's supposed to accept HTML (unless you re-edit the comment I found).

Just for anyone else reading... I think that code might be:

var script:String = "<script><![CDATA[trace(1 < 2 && 2 > 1)]]></script>";

Thanks for bringing this up though Robert, hopefully someone finds this when wondering why the E4X is behaving differently in JSFL compared to in AS3.

Just noticed you posted that code snippet at "13:37"... nice ;)
PermalinkPermalink 02/08/07 @ 14:18
Comment from: Jack Peelsen
Looks like Robert Penner is right!
I was try his test - it's work
PermalinkPermalink 05/08/07 @ 09:53
Comment from: Fabio
Hi,
A small xml is easy using URLVariables.
But my problem is send a big xml.
Using URLVariables the browser says "URL TOO LONG".
Must be another way...
In Adobe Flash forum no one answer this question.
I think Macromedia gave more attention to developers...
PermalinkPermalink 12/02/08 @ 20:35
Comment from: Alexis
+1 for Robert Penner
PermalinkPermalink 28/02/08 @ 14:40

Comments are closed for this post.

About

Richard Leggett is a Freelance RIA and Web Developer (download CV, skills/portfolio coming soon). He is co-author of Foundation Flash Applications for Mobile Devices (Friend of ED), an Adobe Community Expert and speaker at industry conferences and user groups.



July 2008
Mon Tue Wed Thu Fri Sat Sun
 << <   > >>
  1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30 31      

Categories

Miscellany

XML Feeds

Information

Contact the admin  /   b2evo template by Two18 Media