0

我目前正在使用jquery,並且我的整個項目只需使用sharepoint客戶端對象模型(因此我無法使用服務器端編碼)完成。我創建了一個xml結構(通過附加一些字符串)並將其存儲在一個jQuery var變量中。現在我的變量的內容看起來像這樣如何使用jquery將var轉換爲xml內容

<Collection xmlns="http://schemas.microsoft.com/collection/metadata/2009" 
xmlns:ui="http://schemas.microsoft.com/livelabs/pivot/collection/2009" 
SchemaVersion="1" Name="listname"> 
    <FacetCategories> 
     <FacetCategory Name="Title" Type="String" /> 
     <FacetCategory Name="Created By" Type="String" /> 
     <FacetCategory Name="Modified By" Type="String" /> 
    </FacetCategories> 
    <Items ImgBase="http://460d87.dzc"> 
     <Item Id="0" Img="#0" Name="Name1" Href="http://site/1_.000"> 
      <Facets> 
       <Facet Name="Title"> 
        <String Value="Name1" /> 
       </Facet> 
      </Facets> 
     </Item> 
    </Items> 
</collection> 

我想這個變量轉換到完全基於jquery.I XML內容已經使用ParseXml()方法,但我不能看到警報輸出() 。這個你能幫我嗎。

+0

parseXml會給你一個可以查詢的文檔對象,爲什麼你要提醒它 – 2013-03-18 11:21:40

+0

控制檯中的任何錯誤 – 2013-03-18 11:27:34

回答

0

只需使用原生內置XML解析器:

var parser, xml; 
if (window.DOMParser) { 
    parser = new DOMParser(); 
    xml = parser.parseFromString(str, "text/xml"); 
} 
else { // IE 
    xml = new ActiveXObject("Microsoft.XMLDOM"); 
    xml.async = "false"; 
    xml.loadXML(str); 
} 

var nodes = xml.getElementsByTagName('FacetCategory'); 

var i, l = nodes.length, items = []; 
for (i = 0; i < l; i++) { 
    console.log(nodes[i].getAttribute('Name')); 
} 

http://jsfiddle.net/QqtMa/

0

你的XML是無效的,你的根元素是Collection但關閉標籤是collectionc,所以解析器失敗

相關問題