E4X 添加 CDATA 内容
基本上我需要使用变量定义节点名称及其 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
这个怎么样:
输出:
我承认,这不是
CDATA
,但我没有看到问题......解析需要更多时间,但是OTOH,正确转义比CDATA
...带有
XMLNode
的版本使用flash.xml
包,这是为了向后兼容 AS2 ...甚至没有注意到,它在 FP10 下消失了...但是,您可以使用它作为替代品,然后像使用 flash.xml 一样使用
appendChild
...或者您可以使用它 e4x 样式,如果您包装它在一个函数中
,然后
但就我个人而言,我认为基于文本且相对较短的字符串应该被转义,而不是包装在
CDATA
...更新:
我不明白你的意思
这就是整个事情的意义 ... :D .. .
"<"
将在解析过程中被解释,而"<"
只是重新转换为"<"
,所以之后解析 XML,您将得到与之前完全相同相同的字符串...这是我的代码:
在 Flash Player 10 上完美运行,使用 Flex sdk 4 编译...没有flash IDE,但当涉及到纯 ActionScript 结果几乎肯定是相同的,所以它应该可以工作(如果您愿意,您可以将其用作文档类,或者简单地实例化它)...
顺便说一句。 第一个跟踪显示第二个示例有效,这也很明显,因为
new XML()
使用本机XML
解析器来创建XML
来自给定的字符串...这是上面生成的内容:
对我来说效果很好...:)
greetz
back2dos
how about this:
outputs:
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 thanCDATA
...the version with
XMLNode
uses theflash.xml
package, which is for backwards compatibility with AS2 ... didn't even notice, it was gone under FP10 ... however, you could use thisas 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
and then
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
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:
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 nativeXML
parser to create anXML
from the given string ...here is what the above generates:
works quite good for me ... :)
greetz
back2dos
上面的 cdata 函数需要如下所示,注意最后一个“>” 在代码中被转义。 否则会出现编译错误。
The above cdata function needs to look like the following, notice the last ">" is escaped in code. Otherwise there's compile errors.
谢谢,cdata 函数非常有用。 我刚刚写了一篇新的。
Thanks, cdata function is very useful. I wrote just new one.
工作正常:)
work fine :)
这是另一个解决方案
Here is another solution
这是我不使用函数的解决方案:
如果您需要替换现有节点内容并保留节点属性,您可以使用:
Here's my solution without using functions:
If you need to replace existing nodes content and keep node attributes you can use: