简体   繁体   English

如何使用jQuery将var转换为xml内容

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

I am currently working with Jquery and my entire project needs to be done only using sharepoint Client Object Model (so i cant make use of server side coding). 我目前正在使用Jquery,我的整个项目仅需要使用sharepoint客户端对象模型来完成(因此我无法使用服务器端编码)。 I have created a xml structure (by appending some string together) and stored it in a jquery var variable. 我创建了一个xml结构(通过将一些字符串附加在一起)并将其存储在jquery var变量中。 Now my variable content looks like this 现在我的可变内容看起来像这样

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

I want to convert this variable in to xml content purely based on jquery.I have used ParseXml() Method but i'm not able to see the output in alert(). 我想完全基于jquery将此变量转换为xml内容。我使用了ParseXml()方法,但无法在alert()中看到输出。 Please help me out with this. 这个你能帮我吗。

Just use native built-in XML parser: 只需使用本机内置的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/ http://jsfiddle.net/QqtMa/

您的xml无效,您的根元素是Collection而结束标记是带有小c collection ,因此解析器失败

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM