Javascript动态添加cdata部分?

Javascript动态添加cdata部分?,javascript,jquery,xml,append,cdata,Javascript,Jquery,Xml,Append,Cdata,我在处理xml节点属性中存在的特殊字符时遇到问题。为了解决这个问题,我尝试将属性呈现为子节点,并在必要时使用cdata部分来绕过特殊字符。问题是,我似乎无法正确地将cdata部分附加到节点 我正在迭代源xml节点的属性并创建新节点。如果attribute.name=“description”我想将attribute.text()放在cdata节中并附加新节点。那是我跳出跑道的地方 // newXMLData is the new xml document that I've creat

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

我正在迭代源xml节点的属性并创建新节点。如果attribute.name=“description”我想将attribute.text()放在cdata节中并附加新节点。那是我跳出跑道的地方

     // 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 +">"   );  
          }        
        } 
//newXMLData是我在内存中创建的新xml文档
对于(var ctr=0;ctr
有什么想法吗?是否有其他方法添加cdata区域

下面是源代码的一个示例片段

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

这就是我想要创造的

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

4.
测试站点名称
http://www.cnn.com
1.
自2007年6月以来的胡说八道;nbsp;T
&;nbsp<胡说八道;nbsp; 2010-09-20 14:46:18
不确定浏览器是否支持document.implementation.createDocument或createCDATA节,但这至少在Mozilla中有效:

<script>

    // Define some helpers (not available IE < 9)
    function parse (str) {
        return new DOMParser().parseFromString(str, 'text/xml').documentElement;
    }
    function ser (dom) {
        return new XMLSerializer().serializeToString(dom);
    }

    // Simulate your XML retrieval
    var row = '<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;" />';

    // Hack to convert source to well-formed XML, or otherwise you can't use DOM methods on it which
    // depend on well-formed XML
    row = row.replace(/(=\s*")([\s\S]*?)(")/g, function (n0, n1, n2, n3) {
        return n1+ // Add back equal sign and opening quote
            n2.replace(/</g, '&lt;'). // Create well-formed XML by avoiding less-than signs inside attributes
            replace(/&amp;nbsp;/g, '&amp;#160;')+ // HTML entities (except for gt, lt, amp, quot) must be either converted to numeric character references or your XML must define the same entities
            n3; // Add back closing quote
    });

    // Simulate your retrieval of DOM attributes, though in this context, we're just making attributes into a global
    this.attributes = parse(row).attributes;

    // Simulate your creation of an XML document
    var newXMLData = document.implementation.createDocument(null, 'Site', null);

    // Modify your code to avoid jQuery dependency for easier testing and to 
    // avoid confusion (error?) of having two variables, newXMLData and newXMLDataNode
    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;
         var str = "<"+thisNodeName +"></"+ thisNodeName +">";
         var node = parse(str);
         var cdata = newXMLData.createCDATASection(this.attributes[ctr].textContent);
         node.appendChild(cdata);
         newXMLData.documentElement.appendChild(node);       
      }
      else {
       // It's not "Description" so just append the new node.
        var str= "<"+ this.attributes[ctr].name +">" + this.attributes[ctr].textContent + "</"+ this.attributes[ctr].name +">";
        newXMLData.documentElement.appendChild(parse(str));
      }
    } 
    // Prove its working (though you may wish to use toUpperCase() if you need the element names upper-cased); 
    // if you need CDATA for Comments, you can follow the pattern above to add support for that too
    alert(ser(newXMLData)); 
</script>

//定义一些助手(不可用,即<9)
函数解析(str){
返回新的DOMParser().parseFromString(str,'text/xml').documentElement;
}
函数ser(dom){
返回新的XMLSerializer().serializeToString(dom);
}
//模拟XML检索
var行=“”;
//黑客将源代码转换为格式良好的XML,否则就不能在其上使用DOM方法
//依赖格式良好的XML
行=行。替换(/(=\s*”)([\s\s]*?)(”)/g,函数(n0、n1、n2、n3){
return n1+//加回等号和起始引号

n2.replace(/我也遇到了同样的问题。我试图将CDATA附加到xml节点,因此我认为添加如下内容很容易:

valueNode[0].text = "<![CDATA["+ tmpVal +"]]>";
//valueNode[0] represents "<value></value>"
结果将是:

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

Brettz9(在前面的回答中)解释了如何做到这一点,但相当复杂,因此我只想添加更简单的解决方案


谢谢,

您能在添加CDATA之前和之后显示一些XML吗?如果您的XML格式不正确,那么您会遇到两个问题。这并没有真正的帮助。我正在尝试从格式不太好的XML生成格式正确的XML。因此CDATA。
<value><![CDATA[muzi%20test%20002]]></value>