I have an XML which contains a field of the type:
<mytext><![CDATA[ My name is <color value="FF0000">Bill</color>. ]]></mytext>
Since I'm new to E4X methods, I wonder if ther开发者_StackOverflowe is a simple methodology (using E4X methods) in order to print the inner text: "My name is Bill." in a text area and having the word "Bill" colored i.e. red.
The generalized situation is, if i can print the inner text and use XML tags to specify formatting attributes of the text per word.
Do E4X supports this type of parsing, or do I have to program my own "little" parser for this situation?
First, let's normalize the html content (I added a <content>
tag to make it valid):
var mytext:XML = XML("<mytext><![CDATA[<content>My name is <color value="FF0000">Bill</color>.</content>]]></mytext>");
Next step is to parse the given XML:
var roughXML:XML = XML(mytext.text().toString());
Then you have to substitute your custom tags with standard tags:
var output:XML = XML("<span/>");
for each(var tag:XML in roughXML.children())
{
if (tag.name() == "color")
{
var fontTag:XML = XML("<font/>");
fontTag.@color = tag.@value.toString();
fontTag.appendChild(tag.text());
output.appendChild(fontTag);
}
//you can add here any rule for substitution that you need
else
{
output.appendChild(tag);
}
}
And finally, you can use an s:RicheEditableText
to display your text
var textFlow:TextFlow = TextConverter.importToFlow(output.toXMLString(), TextConverter.TEXT_FIELD_HTML_FORMAT);
myRichEditableText.textFlow = textFlow;
精彩评论