• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

PHP simplexml_load with CDATA

Community Expert ,
Oct 02, 2015 Oct 02, 2015

Copy link to clipboard

Copied

Hi everybody!

I'm trying to scrape some data from an XML file that contains content inside CDATA tags and I'm having trouble with it.

feed.XML file:

<?xml version="1.0" encoding="UTF-8"?>

<channel>

     <item>

          <pubDate><![CDATA[Thu, 17 Sep 2015 16:02:53 -0700]]></pubDate>

          <title><![CDATA[Hello world!]]></title>

          <url><![CDATA[http://example.com/?id=hello_world]]></url>

     </item>

</channel>

PHP:

<?php

if (file_exists('feed.xml')) {

    $xml = simplexml_load_file('feed.xml');

     echo $xml->channel->item->pubDate;

} else {

    exit('Failed to open xml.');

}

?>

Error:

Trying to get property of non-object in feed.xml

If I print_r  $xml

I get an array of nodes but no content from [CDATA] tags.

What am I missing?

Nancy O.

Nancy O'Shea— Product User, Community Expert & Moderator

Views

2.3K

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines

correct answers 1 Correct answer

Deleted User
Oct 02, 2015 Oct 02, 2015

It looks like $xml may be returning your "channel" level so instead try echo $xml->item->pubDate;

Also you may have more than one array item. In this case you should specify which array item you want data for, like echo $xml>item[0]->pubDate;

Give either of those suggestions a shot and see if it works. FWIW discussions on php.net for simplexml_load_file() mention print_r not returning content for CDATA but still being able to get it from properly calling it.

Look at example1 at bottom of page at PHP simplexml_load_file() Function

...

Votes

Translate

Translate
Guest
Oct 02, 2015 Oct 02, 2015

Copy link to clipboard

Copied

It looks like $xml may be returning your "channel" level so instead try echo $xml->item->pubDate;

Also you may have more than one array item. In this case you should specify which array item you want data for, like echo $xml>item[0]->pubDate;

Give either of those suggestions a shot and see if it works. FWIW discussions on php.net for simplexml_load_file() mention print_r not returning content for CDATA but still being able to get it from properly calling it.

Look at example1 at bottom of page at PHP simplexml_load_file() Function

Their call is echo $xml->to not echo $xml->note->to

best,

Shocker

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 02, 2015 Oct 02, 2015

Copy link to clipboard

Copied

Thanks much, shocker.  I'll give it a shot.

Have a great weekend!

Nancy O.

Nancy O'Shea— Product User, Community Expert & Moderator

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Oct 03, 2015 Oct 03, 2015

Copy link to clipboard

Copied

LATEST

the_shocker wrote:

It looks like $xml may be returning your "channel" level so instead try echo $xml->item->pubDate;

Right answer. It appears some feeds require the "channel" while others don't.  So that solved my problem. Thanks.

Also you may have more than one array item. In this case you should specify which array item you want data for, like echo $xml>item[0]->pubDate;

Good suggestion.  I was planning on parsing 10 feed items with for($i = 0; $i < 10; $i++){  which works well for what I'm doing here.

And the pubDate date & time stamp looked ugly so I simplified it with $shortDate = date("m/d/Y", strtotime($pubDate));

Overall, good results without a whole lot of code .

Nancy O.

Nancy O'Shea— Product User, Community Expert & Moderator

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines