简体   繁体   English

读取xml文件属性

[英]read xml file attributes

i'm trying to read rss feed from url,i successfully got the title,description etc. 我正在尝试从url读取rss feed,我成功获得了标题,说明等。
Now i'm facing problem while reading attributes info. 现在,我在读取属性信息时面临问题。
my xml is like this: 我的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> 

i can easily access title,description,pub date etc but facing problem while accessing <course:length> <course:id> <course:image-banner> etc 我可以轻松访问标题,说明,发布日期等,但在访问<course:length> <course:id> <course:image-banner> etc

My php code is 我的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>";

} ?> }?>

This is a namespace problem! 这是一个名称空间问题! First, your document must have an xmlns:course="//URL" attribute. 首先,您的文档必须具有xmlns:course="//URL"属性。 Then you can access your <course:*> like this: 然后,您可以像这样访问<course:*>

$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;
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM