E4X 添加 CDATA 内容

发布于 2024-07-29 20:45:37 字数 637 浏览 14 评论 0原文

基本上我需要使用变量定义节点名称及其 CDATA 内容。

var nodeName:String = "tag";
var nodeValue:String = "<non-escaped-content>";

天真地认为这会起作用:

var xml:XML = <doc><{nodeName}><![CDATA[{nodeValue}]]></{nodeName}>

输出:

<doc><tag><![CDATA[{nodeValue}]]></tag></doc>

在为 FP9 设计的脚本的先前版本中,我通过使用绕过了该问题:

new XMLNode( XMLNodeType.XMLNodeType.CDATA_NODE, nodeValue ); // ...

但这似乎在 FP10 中不起作用,而且我感觉该方法无论如何都会被贬值。

有人对此有优雅的解决方案吗?

Basically I need to define a node name and its CDATA content using variables.

var nodeName:String = "tag";
var nodeValue:String = "<non-escaped-content>";

Naively I thought this would work :

var xml:XML = <doc><{nodeName}><![CDATA[{nodeValue}]]></{nodeName}>

Outputs :

<doc><tag><![CDATA[{nodeValue}]]></tag></doc>

In a previous version of the script designed for FP9 I bypassed the problem by using :

new XMLNode( XMLNodeType.XMLNodeType.CDATA_NODE, nodeValue ); // ...

but this doesn't seem to work in FP10, and I have the feeling the method is somehow depreciated anyway.

Anyone an elegant solution for this ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

笔落惊风雨 2024-08-05 20:45:37

这个怎么样:

var xml:XML = <doc><{nodeName}>{nodeValue}</{nodeName}></doc>
trace(xml.toXMLString());

输出:

<doc>
  <tag><non-escaped-content></tag>
</doc>

我承认,这不是 CDATA,但我没有看到问题......解析需要更多时间,但是OTOH,正确转义比 CDATA ...

带有 XMLNode 的版本使用 flash.xml 包,这是为了向后兼容 AS2 ...甚至没有注意到,它在 FP10 下消失了...但是,您可以使用它

var x:XML = new XML("<![CDATA[" + nodeValue + "]]>");

作为替代品,然后像使用 flash.xml 一样使用 appendChild ...

或者您可以使用它 e4x 样式,如果您包装它在一个函数中

function cdata(data:String):XML {
    return = new XML("<![CDATA[" + data + "]]>");
}

,然后

var xml:XML = <doc><{nodeName}>{cdata(nodeValue)}</{nodeName}></doc>

但就我个人而言,我认为基于文本且相对较短的字符串应该被转义,而不是包装在 CDATA ...


更新:
我不明白你的意思

"<""<"

非常不同

这就是整个事情的意义 ... :D .. . "<" 将在解析过程中被解释,而 "<" 只是重新转换为 "<",所以之后解析 XML,您将得到与之前完全相同相同的字符串...

这是我的代码:

package {
    import flash.display.MovieClip;
    public class Main extends MovieClip {       
        public function Main():void {
            var nodeName:String = "tag";
            var nodeValue:String = "<non-escaped-content>";
            var xml:XML = <doc><{nodeName}>{cdata(nodeValue)}</{nodeName}></doc>;
            trace(cdata("test").toXMLString());
            trace(xml.toXMLString());
        }
        private function cdata(data:String):XML {
            return new XML("<![CDATA[" + data + "]]>");
        }
    }
}

在 Flash Player 10 上完美运行,使用 Flex sdk 4 编译...没有flash IDE,但当涉及到纯 ActionScript 结果几乎肯定是相同的,所以它应该可以工作(如果您愿意,您可以将其用作文档类,或者简单地实例化它)...

顺便说一句。 第一个跟踪显示第二个示例有效,这也很明显,因为 new XML() 使用本机 XML 解析器来创建 XML 来自给定的字符串...

这是上面生成的内容:

<![CDATA[test]]>
<doc>
  <tag><![CDATA[<non-escaped-content>]]></tag>
</doc>

对我来说效果很好...:)


greetz

back2dos

how about this:

var xml:XML = <doc><{nodeName}>{nodeValue}</{nodeName}></doc>
trace(xml.toXMLString());

outputs:

<doc>
  <tag><non-escaped-content></tag>
</doc>

i admit, this is not CDATA, but i don't see a problem ... parsing requires a little more time, but OTOH, correct escaping much more robust than CDATA ...

the version with XMLNode uses the flash.xml package, which is for backwards compatibility with AS2 ... didn't even notice, it was gone under FP10 ... however, you could use this

var x:XML = new XML("<![CDATA[" + nodeValue + "]]>");

as a replacement and then use appendChild as you would with flash.xml ...

alternatively you could use it e4x style, if you wrap it in a function

function cdata(data:String):XML {
    return = new XML("<![CDATA[" + data + "]]>");
}

and then

var xml:XML = <doc><{nodeName}>{cdata(nodeValue)}</{nodeName}></doc>

but personally, i think that strings, that are both text based and relatively short, should be escaped, rather then wrapped in CDATA ...


update:
i don't get your point here

"<" is very different than a "<"

that's what the whole thing is about ... :D ... "<" would be interpreted during parsing, whereas "<" is just reconverted to "<", so after parsing the XML, you will have exactly the same string as before ...

this is my code:

package {
    import flash.display.MovieClip;
    public class Main extends MovieClip {       
        public function Main():void {
            var nodeName:String = "tag";
            var nodeValue:String = "<non-escaped-content>";
            var xml:XML = <doc><{nodeName}>{cdata(nodeValue)}</{nodeName}></doc>;
            trace(cdata("test").toXMLString());
            trace(xml.toXMLString());
        }
        private function cdata(data:String):XML {
            return new XML("<![CDATA[" + data + "]]>");
        }
    }
}

works perfectly for me on flash player 10, compiled with flex sdk 4 ... don't have a flash IDE at hand, but when it comes to pure ActionScript results are almost definitely the same, so it should work (you can use that as your document class, if you want to, or simply instantiate it) ...

btw. the first trace shows, that the second example works, which is also quite obvious, since new XML(<String>) uses the native XML parser to create an XML from the given string ...

here is what the above generates:

<![CDATA[test]]>
<doc>
  <tag><![CDATA[<non-escaped-content>]]></tag>
</doc>

works quite good for me ... :)


greetz

back2dos

我只土不豪 2024-08-05 20:45:37

上面的 cdata 函数需要如下所示,注意最后一个“>” 在代码中被转义。 否则会出现编译错误。

private function cdata(data:String):XML
{
    return new XML("<![CDATA[" + data + "]]\>");                
}

The above cdata function needs to look like the following, notice the last ">" is escaped in code. Otherwise there's compile errors.

private function cdata(data:String):XML
{
    return new XML("<![CDATA[" + data + "]]\>");                
}
守护在此方 2024-08-05 20:45:37

谢谢,cdata 函数非常有用。 我刚刚写了一篇新的。

function newNode(nodeName:String,nodeValue:String):XML{
    return new XML(<{nodeName}>{cdata(nodeValue)}</{nodeName}>);
}

Thanks, cdata function is very useful. I wrote just new one.

function newNode(nodeName:String,nodeValue:String):XML{
    return new XML(<{nodeName}>{cdata(nodeValue)}</{nodeName}>);
}
迷荒 2024-08-05 20:45:37
private function cdata(data:String, nodeName:String):XML{
    return new XML( "<"+nodeName+"><![CDATA[" + data + "]]\></"+nodeName+">");
}

工作正常:)

private function cdata(data:String, nodeName:String):XML{
    return new XML( "<"+nodeName+"><![CDATA[" + data + "]]\></"+nodeName+">");
}

work fine :)

葬花如无物 2024-08-05 20:45:37

这是另一个解决方案

public static function getCDATANode(data:String, tagName:String):void
{
        var node:XML  = new XML(  "<" + tagName + "/>" );
        var cdata:XML = new XML("<![CDATA[" + data + " ]]>");
        node.appendChild(cdata);

        trace("getCDATANode: ", node.toXMLString() );
}

Here is another solution

public static function getCDATANode(data:String, tagName:String):void
{
        var node:XML  = new XML(  "<" + tagName + "/>" );
        var cdata:XML = new XML("<![CDATA[" + data + " ]]>");
        node.appendChild(cdata);

        trace("getCDATANode: ", node.toXMLString() );
}
日记撕了你也走了 2024-08-05 20:45:37

这是我不使用函数的解决方案:

var nodeName:String = "tag";
var nodeValue:String = "<non-escaped-content>";
var xml:XML = <doc><{nodeName}>{new XML("<![CDATA[" + nodeValue + "]]>")}</{nodeName}></doc>;

如果您需要替换现有节点内容并保留节点属性,您可以使用:

var newNodeValue:String = "<non-escaped-content>";
var xml:XML = <doc><tag attribute="true">This is node content</tag></doc>;

xml.tag[0].setChildren(new XMLList());
xml.tag[0].appendChild(new XML("<![CDATA[" + newNodeValue + "]]>"));

Here's my solution without using functions:

var nodeName:String = "tag";
var nodeValue:String = "<non-escaped-content>";
var xml:XML = <doc><{nodeName}>{new XML("<![CDATA[" + nodeValue + "]]>")}</{nodeName}></doc>;

If you need to replace existing nodes content and keep node attributes you can use:

var newNodeValue:String = "<non-escaped-content>";
var xml:XML = <doc><tag attribute="true">This is node content</tag></doc>;

xml.tag[0].setChildren(new XMLList());
xml.tag[0].appendChild(new XML("<![CDATA[" + newNodeValue + "]]>"));
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文