2011-01-17 12 views
1

xmlノード属性に存在する特殊文字に問題があります。これに対処するために、属性を子ノードとしてレンダリングしようとしており、必要に応じてcdataセクションを使用して特殊文字を取得しています。問題は、ノードに正しく追加されたcdataセクションを得ることができないということです。JavaScriptをオンザフライで追加するJavascript?

私はソース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セクションを追加する別の方法はありますか?

ここで元のサンプルスニペットだ...ここ

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

CDATAを追加する前後にXMLを表示できますか? – tj111

+0

XMLがうまく構成されていない場合は、2つの問題があります。 – drudge

+0

これは本当に役に立ちません。私はwell-formed xmlからwell-formed xmlを生成しようとしています。したがって、CDATA。 –

答えて

0

ない注意:document.implementation.createDocument用ブラウザのサポートの確認またはcreateCDataSectionですが、これは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> 
2

私は同じ問題がありました。私は、XMLノードにCDATAを追加しようとしていたので、私はそうのような追加などのように簡単に考えた:

valueNode[0].text = "<![CDATA["+ tmpVal +"]]>"; 
//valueNode[0] represents "<value></value>" 

全部がテキストとして解釈されますので、これは動作しませんので、<(未満)と>(より大きい)は自動的に置き換えられます。あなたが何をする必要があるか

は、次の手順を実行して、使用createCDATASectionです:

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(前の回答では)これを行うが、かなり複雑な方法を説明し、したがって、私はずっと簡単な私のソリューションを追加したいだけでした。

ありがとう、

関連する問題