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.
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
...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
Copy link to clipboard
Copied
Thanks much, shocker. I'll give it a shot.
Have a great weekend!
Nancy O.
Copy link to clipboard
Copied
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.