Javascript动态添加cdata部分? [英] Javascript to add cdata section on the fly?

查看:16
本文介绍了Javascript动态添加cdata部分?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到了 xml 节点属性中存在的特殊字符的问题.为了解决这个问题,我尝试将属性呈现为子节点,并在必要时使用 cdata 部分来绕过特殊字符.问题是,我似乎无法正确地将 cdata 部分附加到节点.

I'm having trouble with special characters that exist in an xml node attribute. To combat this, I'm trying to render the attributes as child nodes and, where necessary, using cdata sections to get around the special characters. The problem is, I can't seem to get the cdata section appended to the node correctly.

我正在迭代源 xml 节点的属性并创建新节点.如果 attribute.name = "description" 我想将 attribute.text() 放在 cdata 部分并附加新节点.这就是我跳过赛道的地方.

I'm iterating over the source xml node's attributes and creating new nodes. If the attribute.name = "description" I want to put the attribute.text() in a cdata section and append the new node. That's where I jump the track.

     // newXMLData is the new xml document that I've created in memory
    for (var ctr =0;ctr< this.attributes.length;ctr++){  // iterate over the attributes 
          if( this.attributes[ctr].name =="Description"){   // if the attribute name is "Description" add a CDATA section 
             var thisNodeName =  this.attributes[ctr].name;
               newXMLDataNode.append("<"+thisNodeName +"></"+ thisNodeName +">" );
               var cdata = newXMLData.createCDATASection('test');  // here's where it breaks. 
          } else {
           // It's not "Description" so just append the new node.
            newXMLDataNode.append("<"+ this.attributes[ctr].name +">" + $(this.attributes[ctr]).text() + "</"+ this.attributes[ctr].name +">"   );  
          }        
        } 

有什么想法吗?还有其他方法可以添加 cdata 部分吗?

Any ideas? Is there another way to add a cdata section?

这是源代码的示例片段...

Here's a sample snippet of the source...

<row 
pSiteID="4" 
pSiteTile="Test Site Name " 
pSiteURL="http://www.cnn.com"
ID="1" 
Description="<div>blah blah blah since June 2007.&amp;nbsp; T<br>&amp;nbsp;<br>blah blah blah blah&amp;nbsp; </div>" 
CreatedDate="2010-09-20 14:46:18" 
Comments="Comments example.&#10;" >

这就是我要创建的...

here's what I'm trying to create...

<Site>
<PSITEID>4</PSITEID>
<PSITETILE>Test Site Name</PSITETILE>
<PSITEURL>http://www.cnn.com</PSITEURL>
<ID>1</ID>
<DESCRIPTION><![CDATA[<div>blah blah blah since June 2007.&amp;nbsp; T<br>&amp;nbsp;<br>blah blah blah blah&amp;nbsp; </div ]]></DESCRIPTION>
<CREATEDDATE>2010-09-20 14:46:18</CREATEDDATE>
<COMMENTS><![CDATA[ Comments example.&#10;]]></COMMENTS>
</Site>

推荐答案

我也遇到了同样的问题.我试图将 CDATA 附加到 xml 节点,所以我认为它就像添加这样简单:

I had the same issue. i was trying to append CDATA to xml nodes, so i thought its as easy as adding like so:

valueNode[0].text = "<![CDATA["+ tmpVal +"]]>";
//valueNode[0] represents "<value></value>"

这不起作用,因为整个内容将被解释为文本,因此 <(小于)和 >(大于)将被自动替换.

This does not work because the whole thing will get interpreted as text therefore <(less than) and > (great than) will be replaced automatically.

您需要做的是通过执行以下操作来使用 createCDATASection:

what you need to do is use createCDATASection by doing the following:

var tmpCdata = $xmlDoc[0].createCDATASection(escape("muzi test 002"));
//i'm also escaping special charactures as well
valueNode[0].appendChild(tmpCdata);

结果将是:

<value><![CDATA[muzi%20test%20002]]></value>

Brettz9(在上一个答案中)解释了如何做到这一点,但非常复杂,因此我只想添加我的解决方案,它更简单.

Brettz9 (in previous answer) explains how to do this but quite complex, therefore i just wanted to add my solution which is much simpler.

谢谢,

这篇关于Javascript动态添加cdata部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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