使用jQuery

时间:2017-08-29 15:39:46

标签: javascript jquery xml sharepoint-2010 rss

我正在尝试将RSS Feed解析为数组,但是Feed正在添加CDATA标记并组合某些元素。

我的代码在下面解析rss feed(url)并将某些元素添加到数组中。但是,当我查看Feed本身时,它会在CDATA标记中组合多个关键元素。

如何解析CDATA标记以获取可用的xml字段?

代码

buildXMLDoc = function (url) {
    var list =[];

    $(listXML).find('item').each(function (){
        var el = $(this);
        console.log(el.find("title").text());
        console.log(el.find("pubDate").text());
        console.log(el.find("description").text());
        list.push({title: el.find("title").text(), description: el.find("description").text(), modified: el.find("pubDate").text()});
    });

    return list;

};

XML

<?xml version="1.0" encoding="UTF-8"?>
<!--RSS generated by Microsoft SharePoint Foundation RSS Generator on 8/29/2017 10:23:18 AM -->
<?xml-stylesheet type="text/xsl" href="/_layouts/RssXslt.aspx?List=43aaf08e-0153-4f1d-9b46-e66bba563fde" version="1.0"?>
<rss version="2.0">
  <channel>
    <title>Webdocs: Test</title>
    <description>RSS feed for the Test list.</description>
    <lastBuildDate>Tue, 29 Aug 2017 14:23:18 GMT</lastBuildDate>
    <generator>Microsoft SharePoint Foundation RSS Generator</generator>
    <ttl>60</ttl>
    <language>en-US</language>
    <item>
      <title>Alternative Methods for Determining LCRs</title>
      <description><![CDATA[<div><b>Short Title:</b> Determining LCRs</div>
<div><b>Description:</b> <div class="ExternalClass6280076BC79848078688B86006BA554F"><p>​<span style="font-size:11.5pt;font-family:&quot;calibri&quot;, &quot;sans-serif&quot;">This project is a carryover from the 2017 effort to identify an alternative method for calculating the Locational Minimum Installed Capacity Requirements (LCRs). </span></p></div></div>
<div><b>Governance Process Status:</b> Progress</div>
<div><b>Topic State:</b> Open/Current</div>
<div><b>Updated Placeholder:</b> updated</div>
]]></description>
      <pubDate>Wed, 12 Jul 2017 13:41:06 GMT</pubDate>
    </item>

Console Log:突出显示的项目被认为是单独的元素。

1 个答案:

答案 0 :(得分:1)

为了获得CDATA部分细节,我可能建议使用jquery.contents(),然后通过positon获取相关的子部分。如果职位发生变化,这可能会给你错误的结果,但这是可能的。

&#13;
&#13;
var listXML = '<?xml version="1.0" encoding="UTF-8"?>\
    <!--RSS generated by Microsoft SharePoint Foundation RSS Generator on 8/29/2017 10:23:18 AM -->\
<?xml-stylesheet type="text/xsl" href="/_layouts/RssXslt.aspx?List=43aaf08e-0153-4f1d-9b46-e66bba563fde" version="1.0"?>\
<rss version="2.0">\
        <channel>\
        <title>Webdocs: Test</title>\
<description>RSS feed for the Test list.</description>\
<lastBuildDate>Tue, 29 Aug 2017 14:23:18 GMT</lastBuildDate>\
<generator>Microsoft SharePoint Foundation RSS Generator</generator>\
<ttl>60</ttl>\
<language>en-US</language>\
<item>\
<title>Alternative Methods for Determining LCRs</title>\
<description><![CDATA[<div><b>Short Title:</b> Determining LCRs</div>\
<div><b>Description:</b> <div class="ExternalClass6280076BC79848078688B86006BA554F"><p>​<span style="font-size:11.5pt;font-family:&quot;calibri&quot;, &quot;sans-serif&quot;">This project is a carryover from the 2017 effort to identify an alternative method for calculating the Locational Minimum Installed Capacity Requirements (LCRs). </span></p></div></div>\
<div><b>Governance Process Status:</b> Progress</div>\
<div><b>Topic State:</b> Open/Current</div>\
<div><b>Updated Placeholder:</b> updated</div>\
    ]]></description>\
<pubDate>Wed, 12 Jul 2017 13:41:06 GMT</pubDate>\
</item>';

var list =[];

$(listXML).find('item').each(function (){
    var el = $(this);
    var cdat = $(listXML).find('item description').contents();
    console.log(cdat.eq(1).text() + cdat.eq(2).text());
    console.log(cdat.eq(5).contents().eq(0).text()  + cdat.eq(5).contents().eq(1).text());
    console.log(cdat.eq(6).contents().eq(0).text()  + cdat.eq(6).contents().eq(1).text());
    list.push({title: cdat.eq(2).text(), description: cdat.eq(5).contents().eq(1).text(), modified: cdat.eq(6).contents().eq(1).text()});
});

console.log('list: ' + JSON.stringify(list));
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;

另一种方法是获取description元素,替换内部CDATA并将结果转换为jQuery对象。在此对象上,您可以使用查找来选择子元素。

&#13;
&#13;
var listXML = '<?xml version="1.0" encoding="UTF-8"?>\
    <!--RSS generated by Microsoft SharePoint Foundation RSS Generator on 8/29/2017 10:23:18 AM -->\
<?xml-stylesheet type="text/xsl" href="/_layouts/RssXslt.aspx?List=43aaf08e-0153-4f1d-9b46-e66bba563fde" version="1.0"?>\
<rss version="2.0">\
        <channel>\
        <title>Webdocs: Test</title>\
<description>RSS feed for the Test list.</description>\
<lastBuildDate>Tue, 29 Aug 2017 14:23:18 GMT</lastBuildDate>\
<generator>Microsoft SharePoint Foundation RSS Generator</generator>\
<ttl>60</ttl>\
<language>en-US</language>\
<item>\
<title>Alternative Methods for Determining LCRs</title>\
<description><![CDATA[<div><b>Short Title:</b> Determining LCRs</div>\
<div><b>Description:</b> <div class="ExternalClass6280076BC79848078688B86006BA554F"><p>​<span style="font-size:11.5pt;font-family:&quot;calibri&quot;, &quot;sans-serif&quot;">This project is a carryover from the 2017 effort to identify an alternative method for calculating the Locational Minimum Installed Capacity Requirements (LCRs). </span></p></div></div>\
<div><b>Governance Process Status:</b> Progress</div>\
<div><b>Topic State:</b> Open/Current</div>\
<div><b>Updated Placeholder:</b> updated</div>\
    ]]></description>\
<pubDate>Wed, 12 Jul 2017 13:41:06 GMT</pubDate>\
</item>';

var list =[];

$(listXML).find('item').each(function (){
    var el = $(this);
    var cdat = $(listXML).find('item description').contents();
    var html = $($(listXML).find('item description')[0].innerHTML.replace('<!--[CDATA[', '')).html();
    console.log(html);
});
&#13;
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
&#13;
&#13;
&#13;