simplexml_load_string() is not simple!

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
solid
Forum Commoner
Posts: 28
Joined: Wed Aug 12, 2009 11:56 am

simplexml_load_string() is not simple!

Post by solid »

For 3 hours now I've been trying to figure out how to parse a simple RSS feed with simplexml_load_string(). I cannot figure out how the data is structured or something. I've studied the Manual at php.net, and I've tried many online tutorials, but none of them actually work. I want to scream at the developer that wrote this resulting SimpleXMLElement class, and try to explain to him or her the meaning of the word 'simple'. Any help here would be very very very appreciated. thanks in advance :banghead:

PS All I'm looking for here is an example of the foreach loop that I would use for extracting the title, link, etc. from each item. Thanks again.
User avatar
Weirdan
Moderator
Posts: 5978
Joined: Mon Nov 03, 2003 6:13 pm
Location: Odessa, Ukraine

Re: simplexml_load_string() is not simple!

Post by Weirdan »

Post your xml and the code you use and we will try to help you.
solid
Forum Commoner
Posts: 28
Joined: Wed Aug 12, 2009 11:56 am

Re: simplexml_load_string() is not simple!

Post by solid »

Wow that was a fast reply, thank you. Here is the code which is not working, along with an example xml value.

Code: Select all

				$xml = simplexml_load_string($xml_str);
				if (isset($xml->channel))
				{
					$this->parsed_data = array();
					foreach ($xml->channel->item as $item)
					{
						$obj = new stdClass;
						$obj->title = $item->title;
						$obj->link = $item->link;
						$obj->description = $item->description;
						$obj->pubDate = $item->pubDate;
						$this->parsed_data[] = $obj;
					}
				}

Code: Select all

<?xml version="1.0" encoding="iso-8859-1"?>
<?xml-stylesheet type="text/xsl" href="http://www.somesite.com/content/rss.xsl" ?>
<rss version="2.0" xmlns:coop="http://www.google.com/coop/namespace">
<channel>
<title>somesite.com - stuff </title>
<link>http://www.somesite.com/otherstuff/</link>
<description>A bunch of stuff from somesite.com.</description>
<language>en-us</language>
<pubDate>Wed, 07 Apr 2010 18:04:33 -0400</pubDate>
<lastBuildDate>Wed, 07 Apr 2010 18:04:33 -0400</lastBuildDate>
<docs>http://feedvalidator.org/docs/rss2.html</docs>

<generator>somesite.com Generator 1.0</generator>
<managingEditor>webmaster@somesite.com</managingEditor>
<webMaster>webmaster@someothersite.com</webMaster>
<copyright>Copyright 2004-2010, somesite.com</copyright>
<image>
			<url>http://www.somesite.com/img/somesite.jpg</url>
			<title>somesite.com</title>
			<link>http://www.somesite.com</link>

		</image><item><title>This is a title</title>
<link>http://www.somesite.com/654508.html</link>
<description>This is a description</description>
<pubDate>Wed, 07 Apr 2010 17:58:49 -0400</pubDate>
<guid isPermaLink="false">somesite.com_654508</guid>
<category domain="http://www.somesite.com/stuff/">Big Stuff</category>
<category domain="http://www.somesite.com/stuff/">Little Stuff</category>
<coop:keyword>Big</coop:keyword>

<coop:keyword>Medium</coop:keyword>
<coop:keyword>another size</coop:keyword>

</item>
<item><title>Another title</title>
<link>http://www.somesite.com/654507.html</link>
<description>Another description blah blah</description>
<pubDate>Wed, 07 Apr 2010 17:57:51 -0400</pubDate>

<guid isPermaLink="false">somesite.com_654507</guid>
<category domain="http://www.somesite.com/stuff/">Blah</category>
<category domain="http://www.somesite.com/stuff/">More Blah</category>
<category domain="http://www.somesite.com/stuff/">Even more blah</category>
<category domain="http://www.somesite.com/stuff/">less blah</category>
<category domain="http://www.somesite.com/stuff/">ugh blah</category>
<coop:keyword>Words</coop:keyword>
<coop:keyword>More Words</coop:keyword>

</item>
</channel>
</rss>
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: simplexml_load_string() is not simple!

Post by requinix »

Works fine for me.

I'm guessing

Code: Select all

$obj = new stdClass;
$obj->title = (string)$item->title;
$obj->link = (string)$item->link;
$obj->description = (string)$item->description;
$obj->pubDate = (string)$item->pubDate;
$this->parsed_data[] = $obj;
will fix your problem.
solid
Forum Commoner
Posts: 28
Joined: Wed Aug 12, 2009 11:56 am

Re: simplexml_load_string() is not simple!

Post by solid »

OMG that worked! Thank you.
Post Reply