Usage of escape characters in as 3.0 html and xml in as3

Source: Internet
Author: User
Tags cdata html tags xml parser

When special characters are used in as3, errors may occur accidentally, either the characters are not displayed or the characters are not displayed correctly. In this article, we will make a simple analysis, if you have better analysis, follow up.

Sample. xml and sample_cdata.xml are the xml files used in this example. Please click to view their content. This example uses special characters <and> as an example. The text string variable is txt, the name of the displayed text is shwo_txt.

Because <and> in html and xml have special meanings, they cannot be used directly. When using these two characters, their escape sequences should be used, that is, <and>, therefore, <and> in the xml file will be replaced by & lt; and & gt;. For usage instructions, see sample. xml.

CDATA is used for text data that is not parsed by the xml Parser. In the format of CDATA, the string part is considered a pure string. Even if it contains special characters, it is also considered a common string, for usage, see sample_cdata.xml.

After the xml file is ready, the next step is to use the program to load the xml file and display it in the text box (show_txt) according to the xml content. Here, URLLoader is used for loading. After loading, the string txt is obtained, then, display the information according to the button you clicked. The following is a demo file. "normal" indicates that the xml file of CDATA is not used, and text indicates that the text box does not support html tags, htmlText: html tags are supported (note: flash only supports some html tags ).

As you can see, when xml is normal (CDATA is not used), & lt; and & gt; are first parsed by xml and escaped as <and>, when the text attribute of the string txt is passed to show_txt, because the html tag function is not used, no error occurs and the display result is correct. When the txt is passed to the htmlText attribute of show_txt, because the html tag function is used, special characters <and> are considered invalid characters and are not displayed.

Similarly, when xml uses CDATA to parse text, it considers & lt; and & gt; as simple characters and ignores them, when the txt string is passed to the text attribute of show_txt, & lt; and & gt; are displayed. When the txt string is passed to the htmlText attribute of show_txt, because the html tag function is used, & lt; and & gt; are escaped as <and>, and the result is displayed correctly.

Therefore, both html and xml escape special characters to avoid errors.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.