简体   繁体   中英

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). I have created a xml structure (by appending some string together) and stored it in a jquery var variable. 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(). Please help me out with this.

Just use native built-in XML parser:

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 ,因此解析器失败

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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