Reputation: 27231
I am trying to create a XML variable but I need to include a {
and }
in it.
public class MyClass {
private var simple:XML =
<config>
<url>{PROTOCOL}://{SERVER}/{FOLDER}/</url>
</config>
...
}
Flex is trying to find a variable named PROTOCOL
, SERVER
and FOLDER
. I need to prevent this by escaping the curly brackets.
Question: How can I escape curly braces?
Upvotes: 3
Views: 3183
Reputation: 82028
I have a couple of thoughts:
{
with {
(replacing }
with }
is optional).Upvotes: 4
Reputation: 11
You can also escape the {} w/ a backslash. For example, {} are used in regular expressions, but also denote binding when using RegExpValidator.
<mx:RegExpValidator id="regExpValidator"
expression="[A-Za-z0-9]\{2\}[0-9]\{1,4\}"
flags="gi"
required="true" />
This lets {2} flow through to the expression instead of evaluating to 2.
Upvotes: 0
Reputation: 13003
I would assign the text to a string then bind that string inside the XML - avoids having to escape everything.
public class MyClass {
private var simple:XML = null;
public function MyClass()
{
super();
var s:String = "{PROTOCOL}://{SERVER}/{FOLDER}/";
simple = <config>
<url>{s}</url>
</config>;
}
...
}
If you have to have a lot of text that requires escaping then you might need to look into creating the XML instance from a string as suggested by cwallenpoole or creating a function that will bind the string for a particular element and then appending it to the appropriate parent element.
Upvotes: 3
Reputation: 39408
I would expect if you use a CDATA declaration, this should compile w/o problems. That is the same way MXML allows "non-standard XML" inside a Script tag:
private var simple:XML =
<config>
<url><![CDATA[{PROTOCOL}://{SERVER}/{FOLDER}/]]></url>
</config>
There doesn't seem to be a standard HTML Entity code for the curly bracket; but based on @cwallenpoole answer, you could create it using the ASCII code. { for the open bracket and } for the close bracket:
private var simple:XML =
<config>
<url>{PROTOCOL}://{SERVER}/{FOLDER}</url>
</config>
Upvotes: 2