我正在尝试使用PHP阅读this RSS feed。 XML的小片段:
<?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, <a href="https://onlinelibrary.wiley.com/toc/18630669/2018/46/9">Volume 46, Issue 9</a>, September 2018. <br/></content:encoded>
<rss:description>CLEAN – Soil, Air, Water, Volume 46, Issue 9, September 2018. <br/></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, <a href="https://onlinelibrary.wiley.com/toc/18630669/2018/46/9">Volume 46, Issue 9</a>, September 2018. <br/></content:encoded>
<rss:description>CLEAN – Soil, Air, Water, Volume 46, Issue 9, September 2018. <br/></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>
换句话说,结构是这样的:
rdf:RDF
(some items I don't care about)
rss:item
rss:item
rss:item
我要访问的是那些rss:item对象(一个接一个地执行foreach循环)。我已经尝试了许多不同的版本:
$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-> children()以及$ xml-> {'rss:item'}和许多其他选项,但是我只返回了SimpleXMLElement对象,无法访问信息。绝不包含所有项目的数组。
答案 0 :(得分:1)
如果要输出<rss:item>
数据,那么最简单的方法是获取根元素的children()
,但要从名称空间rss中获得(对$xml->children("rss", true)
的调用) 。这样,您就可以使用对象符号(对于SimpleXML来说是正常的)访问所有数据。
$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
行)(缩写)...
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();
来查看其中包含的内容。