簡體   English   中英

如何使用jQuery將var轉換為xml內容

[英]how to Convert a var to xml content using jquery

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

<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將此變量轉換為xml內容。我使用了ParseXml()方法,但無法在alert()中看到輸出。 這個你能幫我嗎。

只需使用本機內置的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/

您的xml無效,您的根元素是Collection而結束標記是帶有小c collection ,因此解析器失敗

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM