Problems with HTML within XML

english mobile

Around a month ago, I promised more fun and exciting posts about passing XML through ExternalInterface from Authorware to be used within Flex. The specific task I was trying to accomplish is taking a series of xml nodes and displaying their contents on screen using a repeater control. The contents could be images, text, or swfs.

The problem I had was with the text. It seems that Flex is very persnickety about its XML formatting, so each XML node must be indented and on a new line. This is fine for ordinary XML, since who cares how it looks to Flex, but when you have line breaks and tabs added around your <b > tags, that is a problem!

The irritating thing is that the problem occurs not once, but twice! The first time is when the XML gets passed through ExternalInterface. I did eventually find a way to defeat that one, but since it also happens again when you pass the string to the XML() object to make the data source for the repeater, that didn't help much. Flex essentially ignores CDATA tags put into the XML nodes and formats the data anyway, so that didn't help either.

What I eventually wound up doing is replacing the "<" characters in embedded html with "^", then replacing that in the final step before rendering. I'm thinking now that I probably also could have gone with a separate text file for each bit of HTML text and told the Flex file to read that in and display it. However, at the time I didn't know how to get a non-air Flex file to read directly from the hard drive, so it didn't occur to me.

Updated September 18, 2008:
I think this was caused by the prettyPrinting and prettyIndent settings on the global XML object itself. I think that if I'd set prettyPrinting to false, I could have avoided the hassle. One day I may even revisit that component...I'll let you know how it turns out.

9 comments:

Unknown said...

Have you tried using HTML special characters instead of < and >

http://www.utexas.edu/learn/html/spchar.html

Look for less than and more than

Unknown said...

This post was useful. The HTML special characters suggestion really helped.

ManiKanth.

Unknown said...

Amy, any progress? CDATA does not work and the only way to get an object of XML is new XML(String) so you can't set prettyPrint to false and then create it.

Amy B said...

Actually, CDATA does work, but you need to understand that it's very literal, so any line breaks and tab stops in your source XML will be faithfully rendered by Flex. I have an article planed on InsideRIA about how to use this to your advantage.

The object that you set prettyPrinting, ignoreWhiteSpace, prettyIndent, etc., on is the global XML object. So:

private function init() {
   XML.prettyPrinting = false;
   XML.ignoreWhiteSpace = true;
   XML.prettyIndent=0;
}

Unknown said...

Actually, I'm finding (flex sdk 3.0.0) that CDATA isn't literal enough even
with your settings.

For example, &lt in CDATA blocks are being parsed and turned back into tags.

Amy B said...

Have you tried encoding the &?

So &lt;

Amy B said...

Trying again &amp ; lt;

(I added extra spaces so it wouldn't convert.

Unknown said...

Yes, already encoded &lt are being transformed with in XML when wrapped in CDATAs. Wierd.

Unknown said...

Hmm, it still reencodes &amp ; lt ; as the left angle bracket. Seems that Flex's XML parser is just incorrectly ignoring and parsing CDATA no matter what I do.

I'll have to go read the XML reader source, bugs.adobe.com had nothing interesting.