Page 1 of 1
simplexml_load_string() is not simple!
Posted: Wed Apr 07, 2010 4:54 pm
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
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.
Re: simplexml_load_string() is not simple!
Posted: Wed Apr 07, 2010 4:56 pm
by Weirdan
Post your xml and the code you use and we will try to help you.
Re: simplexml_load_string() is not simple!
Posted: Wed Apr 07, 2010 5:13 pm
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>
Re: simplexml_load_string() is not simple!
Posted: Wed Apr 07, 2010 6:10 pm
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.
Re: simplexml_load_string() is not simple!
Posted: Wed Apr 07, 2010 7:17 pm
by solid
OMG that worked! Thank you.