jquery设置xml cdata [英] jquery set xml cdata

查看:98
本文介绍了jquery设置xml cdata的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用jquery设置cdata节点时遇到问题。使用.text()函数很容易获得cdata,但是如果我使用.text('jquery> handy'),它就不会创建cdata节点。

I have a problem setting a cdata node with jquery. Getting cdata is easily done with the .text() function, but if I use .text('jquery > handy') it doesn't create a cdata node.

这是我的程序:
我在xml中获取表单数据以加载形式
如下所示:

This is my procecure: I get form data in xml to load in a form something like this:

<formdata>
  <field id="title"><![CDATA[Some title]]></field>
  <field id="description"><![CDATA[Some description]]></field>
</formdata>

我使用cdata节点,因为一个字段可以包含各种特殊字符。
然后我通过获取节点内容来加载表单中的数据.text()

I use cdata nodes because a field can contain all kinds of special chars. Then I load the data in the form by getting the node content with .text()

如果用户发布表单,我更新xml并转换将它发布到服务器上是一种刺痛。我知道我可以发布字段,但我有一些很好的理由将它放在xml文档中。一切都很好,但如果用户使用特殊字符进行输入则不行。
这是我如何设置节点的值(在本例中为描述节点)

If the user posts the form, I update the xml and convert it to a sting to post it to the server. I know I could just post the fields, but I have some good reasons to put it in an xml document. Everything works very well, but not if the user does some input with special characters. This is how i set the value of the node (in this example the "descriptioon node")

domdoc.find('field[id="description"]').text($("#description").val());

所以节点曾经是cdata,但.text()函数会删除它。我试过这个:

So the node used to be cdata, but the .text() function removes that. I alo tried this:

domdoc.find('field[id="description"]').text('<![CDATA[' + $("#description").val() + ']]>');

这也不起作用,因为.text()更改< to& gt;

This also doesn't work because .text() changes < to &gt;

有没有人有解决方案?我的灵感消失了.....

does anyone has a solution? My inspiration is gone.....

谢谢,
Simon

Thanks, Simon

推荐答案

只需设置文本,无需添加<![CDATA []。 CDATA只是在XML中转义文本的方法之一,但由于jQuery为您逃脱,因此您不需要使用其他方法。 & lt; <![CDATA [<]]> 一样好。对于XML应用程序,它们是相同的。

Just set text, without adding <![CDATA[. CDATA is just one of the ways to escape text in XML, but since jQuery escapes for you, you don't need to use another method. &lt; is just as good as <![CDATA[<]]>. For XML applications they're identical.

这篇关于jquery设置xml cdata的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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