• <bdo id='L66og'></bdo><ul id='L66og'></ul>
  • <legend id='L66og'><style id='L66og'><dir id='L66og'><q id='L66og'></q></dir></style></legend>

  • <small id='L66og'></small><noframes id='L66og'>

        <i id='L66og'><tr id='L66og'><dt id='L66og'><q id='L66og'><span id='L66og'><b id='L66og'><form id='L66og'><ins id='L66og'></ins><ul id='L66og'></ul><sub id='L66og'></sub></form><legend id='L66og'></legend><bdo id='L66og'><pre id='L66og'><center id='L66og'></center></pre></bdo></b><th id='L66og'></th></span></q></dt></tr></i><div id='L66og'><tfoot id='L66og'></tfoot><dl id='L66og'><fieldset id='L66og'></fieldset></dl></div>

        <tfoot id='L66og'></tfoot>

        Javascript动态添加cdata部分?

        Javascript to add cdata section on the fly?(Javascript动态添加cdata部分?)
        <i id='3oioz'><tr id='3oioz'><dt id='3oioz'><q id='3oioz'><span id='3oioz'><b id='3oioz'><form id='3oioz'><ins id='3oioz'></ins><ul id='3oioz'></ul><sub id='3oioz'></sub></form><legend id='3oioz'></legend><bdo id='3oioz'><pre id='3oioz'><center id='3oioz'></center></pre></bdo></b><th id='3oioz'></th></span></q></dt></tr></i><div id='3oioz'><tfoot id='3oioz'></tfoot><dl id='3oioz'><fieldset id='3oioz'></fieldset></dl></div>
      1. <tfoot id='3oioz'></tfoot>
          <bdo id='3oioz'></bdo><ul id='3oioz'></ul>
              <tbody id='3oioz'></tbody>
            • <legend id='3oioz'><style id='3oioz'><dir id='3oioz'><q id='3oioz'></q></dir></style></legend>

                  <small id='3oioz'></small><noframes id='3oioz'>

                  本文介绍了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部分?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  在开发JS过程中,会经常遇到两个小数相运算的情况,但是运算结果却与预期不同,调试一下发现计算结果竟然有那么长一串尾巴。如下图所示: 产生原因: JavaScript对小数运算会先转成二进制,运算完毕再转回十进制,过程中会有丢失,不过不是所有的小数间运算会
                  append() 方法在被选元素的结尾(仍然在内部)插入指定内容。 语法: $(selector).append( content ) var creatPrintList = function(data){ var innerHtml = ""; for(var i =0;i data.length;i++){ innerHtml +="li class='contentLi'"; innerHtml +="a href
                  问题描述: 在javascript中引用js代码,然后导致反斜杠丢失,发现字符串中的所有\信息丢失。比如在js中引用input type=text onkeyup=value=value.replace(/[^\d]/g,) ,结果导致正则表达式中的\丢失。 问题原因: 该字符串含有\,javascript对字符串进行了转
                  Rails/Javascript: How to inject rails variables into (very) simple javascript(Rails/Javascript:如何将 rails 变量注入(非常)简单的 javascript)
                  CoffeeScript always returns in anonymous function(CoffeeScript 总是以匿名函数返回)
                  Ordinals in words javascript(javascript中的序数)
                1. <tfoot id='iVFnC'></tfoot>
                  1. <i id='iVFnC'><tr id='iVFnC'><dt id='iVFnC'><q id='iVFnC'><span id='iVFnC'><b id='iVFnC'><form id='iVFnC'><ins id='iVFnC'></ins><ul id='iVFnC'></ul><sub id='iVFnC'></sub></form><legend id='iVFnC'></legend><bdo id='iVFnC'><pre id='iVFnC'><center id='iVFnC'></center></pre></bdo></b><th id='iVFnC'></th></span></q></dt></tr></i><div id='iVFnC'><tfoot id='iVFnC'></tfoot><dl id='iVFnC'><fieldset id='iVFnC'></fieldset></dl></div>

                    <small id='iVFnC'></small><noframes id='iVFnC'>

                    • <bdo id='iVFnC'></bdo><ul id='iVFnC'></ul>

                      <legend id='iVFnC'><style id='iVFnC'><dir id='iVFnC'><q id='iVFnC'></q></dir></style></legend>
                          <tbody id='iVFnC'></tbody>