在AS3的XML变量中转义Curly Braces

时间:2021-05-06 22:33:53

I am trying to create a XML variable but I need to include a { and } in it.

我正在尝试创建一个XML变量,但我需要在其中包含一个{和}。

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.

Flex正试图找到一个名为PROTOCOL,SERVER和FOLDER的变量。我需要通过转义大括号来防止这种情况。

Question: How can I escape curly braces?

问题:我怎样才能逃避花括号?

4 个解决方案

#1


4  

I have a couple of thoughts:

我有几个想法:

  • You can place the entire thing in a String and then cast the String (see here for getting a multi-line String)
  • 您可以将整个事物放在String中,然后转换String(请参阅此处获取多行字符串)

  • You can use replace the { with &#123; (replacing } with &#125; is optional).
  • 您可以使用替换{with {(replacement} with}是可选的)。

  • Add the node in afterwards (sucks, but technically it is an option)
  • 之后添加节点(很糟糕,但从技术上讲,这是一个选项)

#2


3  

I would assign the text to a string then bind that string inside the XML - avoids having to escape everything.

我将文本分配给一个字符串,然后将该字符串绑定在XML中 - 避免必须逃避一切。

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.

如果你需要有大量需要转义的文本,那么你可能需要考虑从cwallenpoole建议的字符串创建XML实例,或创建一个函数来绑定特定元素的字符串,然后将其附加到适当的元素。父元素。

#3


2  

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:

我希望如果你使用CDATA声明,这应该编译没有问题。这与MXML在Script标记中允许“非标准XML”的方式相同:

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:

似乎没有大括号的标准HTML实体代码;但基于@cwallenpoole答案,您可以使用ASCII代码创建它。 {对于开括号,}为近括号:

private var simple:XML = 
    <config>
        <url>&#123;PROTOCOL&#125;://&#123;SERVER&#125;/&#123;FOLDER&#125;</url>
    </config>

#4


0  

You can also escape the {} w/ a backslash. For example, {} are used in regular expressions, but also denote binding when using RegExpValidator.

您也可以使用反斜杠转义{}。例如,{}用于正则表达式,但也表示使用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.

这允许{2}流向表达式而不是评估为2。

#1


4  

I have a couple of thoughts:

我有几个想法:

  • You can place the entire thing in a String and then cast the String (see here for getting a multi-line String)
  • 您可以将整个事物放在String中,然后转换String(请参阅此处获取多行字符串)

  • You can use replace the { with &#123; (replacing } with &#125; is optional).
  • 您可以使用替换{with {(replacement} with}是可选的)。

  • Add the node in afterwards (sucks, but technically it is an option)
  • 之后添加节点(很糟糕,但从技术上讲,这是一个选项)

#2


3  

I would assign the text to a string then bind that string inside the XML - avoids having to escape everything.

我将文本分配给一个字符串,然后将该字符串绑定在XML中 - 避免必须逃避一切。

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.

如果你需要有大量需要转义的文本,那么你可能需要考虑从cwallenpoole建议的字符串创建XML实例,或创建一个函数来绑定特定元素的字符串,然后将其附加到适当的元素。父元素。

#3


2  

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:

我希望如果你使用CDATA声明,这应该编译没有问题。这与MXML在Script标记中允许“非标准XML”的方式相同:

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:

似乎没有大括号的标准HTML实体代码;但基于@cwallenpoole答案,您可以使用ASCII代码创建它。 {对于开括号,}为近括号:

private var simple:XML = 
    <config>
        <url>&#123;PROTOCOL&#125;://&#123;SERVER&#125;/&#123;FOLDER&#125;</url>
    </config>

#4


0  

You can also escape the {} w/ a backslash. For example, {} are used in regular expressions, but also denote binding when using RegExpValidator.

您也可以使用反斜杠转义{}。例如,{}用于正则表达式,但也表示使用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.

这允许{2}流向表达式而不是评估为2。