0

here's the XML which I'm trying to parse for a while but I'm stuck on nested elements.

 <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>

how to parse course: instructors, my PHP code is

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

EDIT: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++;
}
}

When i print instructor_name and instructor_image sometimes its prints but sometimes it throws warning that main(): Node no longer exists ,how can i check that is empty or not

Magnotta
  • 933
  • 11
  • 34

1 Answers1

0

You can use the children() function to access the child tree of the xml structure.

Do like this:

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

Read: http://php.net/manual/en/simplexmlelement.children.php

But since the XML have multiple nest, you need to use multiple children() function to access the deepest nest.

Here is the modified code to parse the XML you give:

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

Test run: https://eval.in/735810

Afif Zafri
  • 640
  • 1
  • 5
  • 11
  • main(): throwing warning Node no longer exists in line with `$name` – Magnotta Feb 13 '17 at 11:37
  • what main() ? please show your full codes, and errors. Because Based on your given code, the solution work fine. Look at the test run link. – Afif Zafri Feb 13 '17 at 11:39
  • i think if we set the condition to check when $Instructor_name and $instructor_image is empty then this will solve the problem,see http://stackoverflow.com/questions/2502038/simplexml-node-no-longer-exists – Magnotta Feb 13 '17 at 12:01