首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用SimpleXMLElement访问RSS提要中的项

使用SimpleXMLElement访问RSS提要中的项
EN

Stack Overflow用户
提问于 2018-10-04 05:14:58
回答 1查看 768关注 0票数 0

我正在尝试使用PHP读取这个RSS提要。来自XML的小片段:

代码语言:javascript
复制
<?xml version="1.0" encoding="UTF-8"?>
<rdf:RDF xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:cc="http://web.resource.org/cc/"
   xmlns:rss="http://purl.org/rss/1.0/"
   xmlns:content="http://purl.org/rss/1.0/modules/content/"
   xmlns:prism="http://prismstandard.org/namespaces/basic/2.0/"
   xmlns:dc="http://purl.org/dc/elements/1.1/">
   <rss:channel rdf:about="https://onlinelibrary.wiley.com/loi/18630669?af=R">
      <rss:title>Wiley: CLEAN – Soil, Air, Water: Table of Contents</rss:title>
      <rss:description>Table of Contents for CLEAN – Soil, Air, Water. List of articles from both the latest and EarlyView issues.</rss:description>
      <rss:link>https://onlinelibrary.wiley.com/loi/18630669?af=R</rss:link>
      <dc:title>Wiley: CLEAN – Soil, Air, Water: Table of Contents</dc:title>
      <dc:publisher>Wiley</dc:publisher>
      <dc:language>en-US</dc:language>
      <prism:publicationName>CLEAN – Soil, Air, Water</prism:publicationName>
      <rss:items>
         <rdf:Seq>
            <rdf:li rdf:resource="https://onlinelibrary.wiley.com/doi/abs/10.1002/clen.201800305?af=R"/>
            <rdf:li rdf:resource="https://onlinelibrary.wiley.com/doi/abs/10.1002/clen.201700117?af=R"/>
         </rdf:Seq>
      </rss:items>
   </rss:channel>
   <rss:image rdf:about="http://www.atypon.com/images/atypon_logo_small.gif">
      <rss:title>CLEAN – Soil, Air, Water</rss:title>
      <rss:url>http://www.atypon.com/images/atypon_logo_small.gif</rss:url>
      <rss:link>https://onlinelibrary.wiley.com/loi/18630669?af=R</rss:link>
   </rss:image>
   <rss:item rdf:about="https://onlinelibrary.wiley.com/doi/abs/10.1002/clen.201800305?af=R">
      <rss:title>The Limiting Factor to the Outbreak of Lake Black Bloom: Roles of Ferrous Iron and Sulfide Ions</rss:title>
      <dc:description>
         abc
      </dc:description>
      <dc:creator>
         Qiushi Shen, 
         Chengxin Fan, 
         Cheng Liu, 
         Chao Chen
      </dc:creator>
      <rss:link>https://onlinelibrary.wiley.com/doi/abs/10.1002/clen.201800305?af=R</rss:link>
      <content:encoded>CLEAN – Soil, Air, Water, &lt;a href="https://onlinelibrary.wiley.com/toc/18630669/2018/46/9"&gt;Volume 46, Issue 9&lt;/a&gt;, September 2018. &lt;br/&gt;</content:encoded>
      <rss:description>CLEAN – Soil, Air, Water, Volume 46, Issue 9, September 2018. &lt;br/&gt;</rss:description>
      <dc:title>The Limiting Factor to the Outbreak of Lake Black Bloom: Roles of Ferrous Iron and Sulfide Ions</dc:title>
      <dc:identifier>doi:10.1002/clen.201800305</dc:identifier>
      <dc:source>CLEAN – Soil, Air, Water</dc:source>
      <dc:date>2018-08-19T07:00:00Z</dc:date>
      <prism:publicationName>CLEAN – Soil, Air, Water</prism:publicationName>
      <prism:volume>46</prism:volume>
      <prism:number>9</prism:number>
      <prism:coverDate>2018-08-19T07:00:00Z</prism:coverDate>
      <prism:coverDisplayDate>2018-08-19T07:00:00Z</prism:coverDisplayDate>
      <prism:doi>10.1002/clen.201800305</prism:doi>
      <prism:url>https://onlinelibrary.wiley.com/doi/abs/10.1002/clen.201800305?af=R</prism:url>
      <prism:copyright/>
   </rss:item>
   <rss:item rdf:about="https://onlinelibrary.wiley.com/doi/abs/10.1002/clen.201700117?af=R">
      <rss:title>A Pilot‐Scale Diatomite Membrane Bioreactor for Slightly Polluted Surface Water Treatment</rss:title>
      <dc:description>
         abc
      </dc:description>
      <dc:creator>
         Wen Sun, 
         Cuimei Li, 
         Bingzhi Dong, 
         Huaqiang Chu
      </dc:creator>
      <rss:link>https://onlinelibrary.wiley.com/doi/abs/10.1002/clen.201700117?af=R</rss:link>
      <content:encoded>CLEAN – Soil, Air, Water, &lt;a href="https://onlinelibrary.wiley.com/toc/18630669/2018/46/9"&gt;Volume 46, Issue 9&lt;/a&gt;, September 2018. &lt;br/&gt;</content:encoded>
      <rss:description>CLEAN – Soil, Air, Water, Volume 46, Issue 9, September 2018. &lt;br/&gt;</rss:description>
      <dc:title>A Pilot‐Scale Diatomite Membrane Bioreactor for Slightly Polluted Surface Water Treatment</dc:title>
      <dc:identifier>doi:10.1002/clen.201700117</dc:identifier>
      <dc:source>CLEAN – Soil, Air, Water</dc:source>
      <dc:date>2018-08-24T07:00:00Z</dc:date>
      <prism:publicationName>CLEAN – Soil, Air, Water</prism:publicationName>
      <prism:volume>46</prism:volume>
      <prism:number>9</prism:number>
      <prism:coverDate>2018-08-24T07:00:00Z</prism:coverDate>
      <prism:coverDisplayDate>2018-08-24T07:00:00Z</prism:coverDisplayDate>
      <prism:doi>10.1002/clen.201700117</prism:doi>
      <prism:url>https://onlinelibrary.wiley.com/doi/abs/10.1002/clen.201700117?af=R</prism:url>
      <prism:copyright/>
   </rss:item>

换句话说,结构是这样的:

代码语言:javascript
复制
  rdf:RDF
    (some items I don't care about)
    rss:item
    rss:item
    rss:item

我试图访问的是那些rss:item对象(一个接一个地执行一个foreach循环)。我尝试过许多不同版本的这种方法:

代码语言:javascript
复制
$url = "https://onlinelibrary.wiley.com/action/showFeed?jc=18630669&type=etoc&feed=rss";
$xml = file_get_contents($url);
$xml = new SimpleXMLElement($xml);

$var = $xml->{'rdf:RDF'};

但它只返回object(SimpleXMLElement)[598]。我没有办法进入这些物品。我尝试使用$xml->子()以及$xml->{'rss:item'}和许多其他选项,但我只返回了SimpleXMLElement对象,无法访问信息;从来没有一个包含所有项的数组。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2018-10-04 05:30:45

如果您想输出<rss:item>数据,那么最简单的方法是获取根元素的children(),但要从名称空间前缀rss (调用$xml->children("rss", true))获得。然后,这将允许您使用对象表示法访问所有数据(这是SimpleXML的正常做法)。

代码语言:javascript
复制
$url = "https://onlinelibrary.wiley.com/action/showFeed?jc=18630669&type=etoc&feed=rss";
$xml = file_get_contents($url);
$xml = new SimpleXMLElement($xml);

foreach ( $xml->children("rss", true)->item as $item ) {
    echo (string)$item->title.PHP_EOL;
}

输出每个项目的标题元素( echo (string)$item->title.PHP_EOL行)(缩写)。

代码语言:javascript
复制
The Limiting Factor to the Outbreak of Lake Black Bloom: Roles of Ferrous Iron and Sulfide Ions
A Pilot‐Scale Diatomite Membrane Bioreactor for Slightly Polluted Surface Water Treatment
Agronomic Valorization of Olive Mill Wastewaters: Effects on Medicago sativa Growth and Soil Characteristics

需要注意的一点是,您说只返回object(SimpleXMLElement)[598] --但是SimpleXMLElement可能包含一个元素列表,更多的是说明您如何使用该内容。此外,使用print_r()或许多其他正常的方式查看内容并不能提供完整的数据。对于SimpleXML --使用echo $xml->asXML();查看它包含的内容。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/52639450

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档