2016-07-12 11 views
0

URLからrssフィードを読み込もうとしています。タイトル、説明などが見つかりました。
属性情報の読み取り中に問題が発生しました。
私のXMLは、このようなものです:xmlファイルの属性を読み取る

<item> 
<guid>https://www.edx.org/node/22726</guid> 
<title>Managing Projects with Microsoft Project</title> 
<link> 
https://www.edx.org/course/managing-projects-microsoft-project-microsoft-cld213x 
</link> 
<description> 
Want to master project management? Have a project to manage but unsure where to begin? With over 20 million users, Microsoft Project is the go to app for project managers. 
</description> 
<pubDate>Wed, 06 Jul 2016 16:21:40 -0400</pubDate> 
<course:id>course-v1:Microsoft+CLD213x+2T2016</course:id> 
<course:code>CLD213x</course:code> 
<course:created>Thu, 16 Jun 2016 14:59:55 -0400</course:created> 
<course:start>2016-07-11 00:00:00</course:start> 
<course:end>2016-12-31 00:00:00</course:end> 
<course:self_paced>0</course:self_paced> 
<course:length>6 modules</course:length> 
<course:prerequisites> 
Basic project management knowledge and skills Basic knowledge and skills using any current Windows® operating system (preferably Windows 10) Competency in using other Microsoft® Office® applications (preferably Office 2016) 
</course:prerequisites> 
</item> 

<course:length> <course:id> <course:image-banner> etc

私のPHPコードである

<?php 
$rss = simplexml_load_file('https://www.example.org/api/v2/report/course-feed/rss'); 
echo '<h1>'. $rss->channel->title . '</h1>'; 
foreach ($rss->channel->item as $item) { 
echo '<h2><a href="'. $item->link .'">' . $item->title . "</a></h2>"; 
echo "<p>" . $item->pubDate . "</p>"; 
echo "<p>" . $item->description . "</p>"; 

} にアクセスしている間、私は簡単にタイトル、説明、パブの日付などが、直面する問題にアクセスすることができますか? >

答えて

2

これは名前空間の問題です。まず、文書にはxmlns:course="//URL"属性が必要です。

$rss = simplexml_load_file('https://www.example.org/api/v2/report/course-feed/rss'); 
$namespaces = $rss->getNamespaces(true);//Add this line 
echo '<h1>'. $rss->channel->title . '</h1>'; 
foreach ($rss->channel->item as $item) { 
    echo '<h2><a href="'. $item->link .'">' . $item->title . "</a></h2>"; 
    echo "<p>" . $item->pubDate . "</p>"; 
    echo "<p>" . $item->description . "</p>"; 

    $course = $item->children($namespaces['course']); 
    echo $course->id; 
    echo $course->prerequisites; 
} 
+0

私はこの答えを追加しました:) 30秒で遅くなった1 –

+0

@ChetanAmetaこれは、時間の80%のように私に何が起こる:Dそして、あなたは、このようなあなたの<course:*>にアクセスすることができます –

関連する問題