【发布时间】:2011-01-17 19:21:20
【问题描述】:
我遇到了 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 +">" );
}
}
有什么想法吗?还有其他方法可以添加 cdata 部分吗?
这是源代码的示例 sn-p...
<row
pSiteID="4"
pSiteTile="Test Site Name "
pSiteURL="http://www.cnn.com"
ID="1"
Description="<div>blah blah blah since June 2007.&nbsp; T<br>&nbsp;<br>blah blah blah blah&nbsp; </div>"
CreatedDate="2010-09-20 14:46:18"
Comments="Comments example. " >
这就是我要创建的...
<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.&nbsp; T<br>&nbsp;<br>blah blah blah blah&nbsp; </div ]]></DESCRIPTION>
<CREATEDDATE>2010-09-20 14:46:18</CREATEDDATE>
<COMMENTS><![CDATA[ Comments example. ]]></COMMENTS>
</Site>
【问题讨论】:
-
能否在添加 CDATA 前后显示一些 XML?
-
如果您的 XML 格式不正确,那么您有两个问题..
-
这并没有真正的帮助。我正在尝试从格式不正确的 xml 生成格式正确的 xml。因此是 CDATA。
标签: javascript jquery xml append cdata