使用php解析嵌套的xml

时间:2017-02-13 09:15:55

标签: php xml

这里是我试图解析一段时间的XML,但是我坚持使用嵌套元素。

 <rss xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:course="https://www.example.org/api/course/elements/1.0/" xmlns:staff="https://www.example.org/api/staff/elements/1.0/" version="2.0">
<channel>
<item>
<title>example.org course feed</title>
<link>https://www.example.org/api/v2/report/course-feed/rss</link>
<description>.org - course catalog feed</description>
<language>en</language>
 <course:instructors>
   <course:staff>
     <staff:name>Mark Moran</staff:name>
   </course:staff>
 </course:instructors>
 </item>
</channel>

如何解析课程:教师,我的PHP代码是

$rss = simplexml_load_file('https://www.edx.org/api/v2/report/course-feed/rss');
$namespaces = $rss->getNamespaces(true);
foreach ($rss->channel->item as $item) {
$title = $item->title ;
}

修改:2

 $rss = simplexml_load_file('https://www.example.org/api/v2/report/course-feed/rss');
 $namespaces = $rss->getNamespaces(true);//Add this line
 foreach ($rss->channel->item as $item) {
 $course_title = $item->title ;
 $course_description = $item->description;
 $course_url = $item->link;
 $course = $item->children($namespaces['course']);
 $course_thumbnail_url = $course->{'image-thumbnail'};
 $course_banner_url = $course->{'image-banner'};
 $course_teaser_url = $course->{'video-youtube'};
 $course_start_date = $course->start;
 $course_duration = $course->length;
 $instructors = $item->children('course',true)->instructors;
 $staff = $instructors->children('course',true)->staff;
 $instructor_name = $staff->children('staff',true)->name;
 $instructor_image = $staff->children('staff',true)->image;
 echo $instructor_name.' '.$instructor_image,"<br>";
 $course_price = 0;
 $course_provider_id = 3;
 $course_affiliates = $course->school;
 $categories = $course->subject;
 $categories = explode(',', $categories);
 $c = count($categories);
 $i = 0;
 while($i < $c)
 {
  $course_rating = mt_rand(3.5,5);
  $course_category = $categories[$i];
  if(mysqli_query($conn,"INSERT into course_catalog_table (course_title,course_description,course_url,course_thumbnail_url,course_banner_url,course_teaser_url,course_category,course_start_date,course_duration,course_rating,course_affiliates,course_instructor,course_instructor_image,course_price,course_provider_id) VALUES('$course_title','$course_description','$course_url','$course_thumbnail_url','course_banner_url','course_teaser_url','$course_category','$course_start_date','$course_duration','$course_rating','$course_affiliates','$instructor_name',$instructor_image','$course_price')"))
  {
      echo "successfull\r\n";
 }
 $i++;
}
}

当我打印instructor_name和instructor_image时,有时会打印出来但有时会抛出main(): Node no longer exists的警告,我该怎么检查它是否为空

1 个答案:

答案 0 :(得分:0)

您可以使用children()函数访问xml结构的子树。

这样做:

$rss->channel->item->children('course',true)->instructors;

阅读:http://php.net/manual/en/simplexmlelement.children.php

但由于XML具有多个嵌套,因此您需要使用多个children()函数来访问最深的嵌套。

以下是用于解析您提供的XML的修改代码:

foreach ($rss->channel->item as $item) 
{
    $title = $item->title;

    // access course:instructors nest
    $instructors = $item->children('course',true)->instructors;

    // then access the course:staff nest
    $staff = $instructors->children('course',true)->staff;

    // finally access the staff:name nest value
    $name = $staff->children('staff',true)->name;

    // print the value
    echo "Staff Name: ". $name . "<br>";
}

试运行:https://eval.in/735810