Skip to main content
Nancy OShea
Community Expert
Community Expert
October 2, 2015
Answered

PHP simplexml_load with CDATA

  • October 2, 2015
  • 1 reply
  • 2611 views

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.

This topic has been closed for replies.
Correct answer

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

1 reply

Correct answer
October 2, 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

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

best,

Shocker

Nancy OShea
Community Expert
Community Expert
October 2, 2015

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

Have a great weekend!

Nancy O.

Nancy O'Shea— Product User & Community Expert