JavaScript来动态添加CDATA节? [英] Javascript to add cdata section on the fly?

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

问题描述

我在使用中存在的XML节点属性的特殊字符的麻烦。为了解决这个问题,我想呈现的属性子节点,并在必要时使用CDATA节来解决的特殊字符。问题是,我似乎无法得到追加到正确的节点CDATA部分。

我遍历源XML节点的属性和创建新的节点。如果attribute.name =说明我要把attribute.text()在CDATA部分并追加新的节点。这就是我跳的轨道。

  // newXMLData是,我已经在内存中创建新的XML文档
    对于(VAR CTR = 0;&CTR LT; this.attributes.length; CTR ++){//遍历属性
          如果(this.attributes [CTR]。名称==说明){//如果属性名称是说明添加CDATA节
             VAR thisNodeName = this.attributes [CTR]。名称;
               newXMLDataNode.append(&下;+ thisNodeName +>&下; /+ thisNodeName +>中);
               VAR CDATA = newXMLData.createCDATASection('测试'); //这里的地方它打破。
          }其他{
           //这不是描述所以只需追加新的节点。
            newXMLDataNode.append(<+ this.attributes [点击率]。名称+>中。+ $(this.attributes [CTR])文本()+< /+ this.attributes [点击率。名+>中);
          }
        }

任何想法?是否有另一种方式来增加CDATA节?

这里的源代码的示例代码段...

 <排
pSiteID =4
pSiteTile =测试站​​点名
pSiteURL =htt​​p://www.cnn.com
ID =1
说明=< D​​IV>等等等等自2007年6月和放大器;放大器; NBSP; T< BR>&放大器;放大器; NBSP;< BR>等等等等等等等等&放大器;放大器; NBSP;< / DIV>中
CreatedDate =2010-09-20 14时46分18秒
评论=点评例如&放大器;#10; >

这里就是我试图创建...

 <&网站GT;
&所述; PSITEID→4&下; / PSITEID>
< PSITETILE>测试站点名称和LT; / PSITETILE>
&LT; PSITEURL&GT; HTTP://www.cnn.com< / PSITEURL&GT;
&LT; ID&GT; 1 LT; / ID&GT;
&LT;说明&GT;&LT;![CDATA [&LT; D​​IV&GT;放大器;自2007年6月和放大器等等等等NBSP; T&LT; BR&GT;&放大器;放大器; NBSP;&LT; BR&GT;等等等等等等等等&放大器;放大器; NBSP; &LT; / DIV]&GT;&LT; /描述&GT;
&LT; CREATEDDATE&GT; 2010-09-20 14:46:18&LT; / CREATEDDATE&GT;
&LT;意见&GT;&LT;![CDATA [评论例如&放大器;#10;]]&GT;&LT; /意见&GT;
&LT; /网站&GT;


解决方案

我有同样的问题。我试图追加到CDATA XML节点,所以我想它的那么容易,因为添加像这样:

  valueNode [0]的.text =&LT;![CDATA [+ tmpVal +]]&gt;中;
// valueNode [0]再presents&LT; VALUE&GT;&LT; /值&gt;中

这不起作用,因为整个事情会得到PTED为因此文字&lt间$ P $(小于)和>(比大)会被自动替换

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

  VAR tmpCdata = $ xmlDoc中,[0] .createCDATASection(逃生(木子测试002));
//我也转义特殊charactures以及
valueNode [0] .appendChild(tmpCdata);

结果将是:

 &LT; VALUE&GT;&LT;![CDATA [木子%20test%20002]&GT;&LT; /值&GT;

Brettz9(以previous答案)解释如何做到这一点,但比较复杂,所以我只是想补充我的解决方案,它要简单得多。

感谢,

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.

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

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>

解决方案

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.

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);

results will be:

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

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

thanks,

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

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