php - 无法解析xml文件属性

时间:2016-11-10 12:53:04

标签: php xml rss

我有以下xml文件但在解析staff list的属性时遇到问题。如何获取staff:name和'staff:image`。

<rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:course="https://www.edx.org/api/course/elements/1.0/" xmlns:staff="https://www.edx.org/api/staff/elements/1.0/" version="2.0">
<channel>
 <title>edX.org course feed</title>
 <link>https://www.edx.org/api/v2/report/course-feed/rss</link>
 <atom:link rel="first" href="https://www.edx.org/api/v2/report/course-feed/rss"/>
 <atom:link rel="last" href="https://www.edx.org/api/v2/report/course-feed/rss?page=12"/>
 <atom:link rel="previous" href="https://www.edx.org/api/v2/report/course-feed/rss?page=11"/>
 <atom:link href="https://www.edx.org/api/v2/report/course-feed/rss" rel="self" type="application/rss+xml"/>
 <description>edX.org - course catalog feed</description>
 <language>en</language>
 <item>
 <title>Quantum Mechanics of Molecular Structures</title>
 <course:instructors>
  <course:staff>
   <staff:name>Kaoru Yamanouchi</staff:name>
   <staff:title/>
   <staff:bio>
     Professor Kaoru Yamanouchi has been Professor of Chemistry at the University of Tokyo since April 1997. His research fields are in physical chemistry; especially, gas phase laser spectroscopy, chemical reaction dynamics, and intense laser science. He is one of the world-leading scientists in the new interdisciplinary research field called ultrafast intense laser science. He has been the recipient of Morino Fellowship endowed by Morino Foundation (1987), Spectroscopical Society of Japan Award for High-Quality Papers (1989), Chemical Society of Japan Award for Young Scientists (1991), Japan IBM Prize (2000), The Best Paper Award of the Laser Society of Japan (2008), and Chemical Society of Japan Award (2015).
   </staff:bio>
   <staff:image>
    https://www.edx.org/sites/default/files/person/image/photoyamanouchi110x110.jpg
   </staff:image>
   </course:staff>
 <course:staff>
</course:instructors>
</item>
</channel>
</rss>

我如何解析course:instructors列表 到目前为止,我的代码是:

 $rss = simplexml_load_file('https://www.edx.org/api/v2/report/course-feed/rss?page=12');
 $namespaces = $rss->getNamespaces(true);//Add this line
 echo '<h1>'. $rss->channel->title . '</h1>';
 foreach ($rss->channel->item as $item) {
   $title = $item->title ;
   echo '<h2><a href="'. $item->link .'">' . $title . "</a></h2>";
   $course = $item->children($namespaces['course']);
 }

0 个答案:

没有答案