E4X添加CDATA内容 [英] E4X Add CDATA content

查看:273
本文介绍了E4X添加CDATA内容的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

基本上,我需要使用变量来定义一个节点名称和它的CDATA内容。

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

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脚本的previous的版本,我用绕过这个问题:

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

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

不过这似乎并没有在FP10的工作,和我有感觉的方法是有点反正pciated德$ P $。

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 ?

推荐答案

这个怎么样:

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

输出:

<doc>
  <tag>&lt;non-escaped-content&gt;</tag>
</doc>

我承认,这不是 CDATA ,但我没有看到一个问题...解析需要多一点的时间,但OTOH,正确的逃逸更健壮比 CDATA ...

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 ...

的XMLNode 的版本使用 flash.xml 包,这是为了和AS2兼容...甚至没有注意到,这是FP10下消失了......不过,你可以使用这个

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 + "]]>");

作为替代,然后使用的appendChild ,你会与flash.xml ...

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

另外,您可以使用它E4X的风格,如果你在一个函数把它包

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

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

然后

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

不过,我个人认为,字符串,是基于文本和相对较短的,应当逃了出来,而不是包裹在 CDATA ...


更新: 我不明白你的意思在这里

update: i don't get your point here

&放大器; LT;是不是非常不同的一个&LT;

"&lt;" is very different than a "<"

这就是整个事情是怎么样 ...:D ... &LT;将在PTED跨$ P $解析,而&放大器; LT;只是重新转换为&LT;,所以解析XML后,你将有完全作为前相同的字符串...

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

这是我的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 + "]]>");
    	}
    }
}

完全适用于我的flash播放器10,用的Flex SDK 4编...不要手头闪光的IDE,但是当涉及到​​纯ActionScript结果几乎肯定是一样的,所以它应该工作(你可以使用它作为您的文档类,如果你想,或者干脆将它实例化)...

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。第一个跟踪显示,第二示例工作,这也是相当明显的,因为新的XML(小于字符串&GT;)使用本机 XML 解析器来创建一个 XML 从给定字符串...

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 ... :)


这篇关于E4X添加CDATA内容的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆