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

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

                  相关文档推荐

                  Toggle HTML radio button by clicking its label(通过单击标签来切换 HTML 单选按钮)
                  Javascript how to change radio button label text?(Javascript如何更改单选按钮标签文本?)
                  JavaScript radio button confirmation(JavaScript 单选按钮确认)
                  How can I automatically select specific radio buttons with Greasemonkey?(如何使用 Greasemonkey 自动选择特定的单选按钮?)
                  AngularJs. Is it possible to deselect HTML “radio” input by click?(AngularJs.是否可以通过单击取消选择 HTML“收音机输入?)
                  Checking Value of Radio Button Group via 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>