Recipe 20.15. Using HTML and Special Characters in XML


Problem

You want to use HTML or other text that uses characters with specific meaning in the context of XML.

Solution

Use a CDATA tag to enclose the text.

Discussion

While this recipe doesn't deal with anything specific to ActionScript per se, it is relevant in that it addresses something you'll likely encounter when working with XML with respect to Flash or otherwise. In XML, certain characters are interpreted in specific ways. For example, the greater-than and less-than characters are interpreted as the delimiters of XML tags. If you try to use one of those characters as part of some group of text within the XML document, it causes parser errors when you try to use the document; for example:

<example>a < b</example>

As you can see, the preceding encloses the text a < b in a text node. However, because the less-than character is interpreted in a specific way in the context of an XML document, the example causes a parser error. Another common example is one in which you want to store some HTML within an XML document; for example:

<htmlExample><a href="http://www.darronschall.com">Darron</a></htmlExample>

In such a case the HTML is interpreted as XML, not as a string. The preceding XML packet contains <htmlExample> as the root node, with a child <a> node that contains a text node with the string value of Darron.

In any case, you can enclose text in a CDATA (Character Data) tag, which won't be interpreted by the XML parser. Instead, the parser simply treats the enclosed data as a string.

A CDATA tag begins with <![CDATA[ and it ends with ]]>. Therefore, the first example can be written as follows:

<example><![CDATA[a < b]]></example>

The second example can be rewritten as follows:

<htmlExample><![CDATA[<a href="http://www.darronschall.com">Darron</a>]]></htmlExample>

When the XML is parsed, the character data inside the CDATA tag is treated as a single string, but the CDATA tag itself is discarded. So, for example, the text node in the first example would be retrieved as a < b, not <![CDATA[a < b]]>. In the second example, the XML packet is interpreted as a root node of <htmlExample>, with a single text node containing the entire HTML link as a string. Because of the CDATA tag, the <a> is not interpreted as a child node as it was previously, and instead is just part of the string value of the text node.




ActionScript 3. 0 Cookbook
ActionScript 3.0 Cookbook: Solutions for Flash Platform and Flex Application Developers
ISBN: 0596526954
EAN: 2147483647
Year: 2007
Pages: 351

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net