dfowler Posted February 4, 2008 Share Posted February 4, 2008 Hey guys, my friend wants me to create some reports for him (out of list of names how many are male, female, ect..). The problem is all the information is from RSS feeds. I have never worked with RSS or XML ever before. Is is possible to pull pieces of information out of feeds? If so, how do I do that? Here is an example of the feed: <?xml version="1.0" encoding="UTF-8"?> <rss version="2.0" xmlns:ddb="http://site.com/schema"> <channel> <title>abc</title> <link>http://site.com/publish/name/app/abc.html</link> <description></description> <item> <title>1</title> <link>http://site/app/name?view=98619&entry=97919</link> <description><b>Number: </b>1<br><b>District: </b>SD<br><b>Name: </b>Bob Jenkins, M<br><b>Male: </b>1<br><b>Female: </b>0</description> <pubDate>Fri, 01 Feb 08 18:54:00 -0500</pubDate> <ddb:female>0</ddb:female> <ddb:district>SD</ddb:district> <ddb:name>Bob Jenkins, M</ddb:name> <ddb:number>1</ddb:number> <ddb:male>1</ddb:male> </item> </channel> </rss> Quote Link to comment Share on other sites More sharing options...
rhodesa Posted February 4, 2008 Share Posted February 4, 2008 Check out SimpleXML, it's the easiest way to deal with basic XML Quote Link to comment Share on other sites More sharing options...
dfowler Posted February 4, 2008 Author Share Posted February 4, 2008 Ok reading that it appears I can do what I need. I just need to figure it out. The report will basically be a page with a table loaded. The table will be something similar to this: District Male Female district name # male # female I think there is something like 5 districts. Quote Link to comment Share on other sites More sharing options...
dfowler Posted February 4, 2008 Author Share Posted February 4, 2008 Ok, I'm having trouble getting it to display the correct information. Here is what I have so far just to test: <?php $url = 'http://site.com/publish/davis/info/abc.rss'; $rss_file = file_get_contents($url); $xml = new SimpleXMLElement($rss_file); echo $xml->channel->item[0]->ddb:male; ?> I'm trying to follow what is going on with the SimpleXML, but since this is something I've never done before I am starting to get lost. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted February 4, 2008 Share Posted February 4, 2008 The ddb: registers values in a particular 'namespace'. To access those values, you need to get the children for that particular namespace: <?php $url = 'http://site.com/publish/davis/info/abc.rss'; $xml = simplexml_load_file($url); $ddb_ns = 'http://site.com/schema'; echo $xml->channel->item->children($ddb_ns)->male; ?> Also, just a note. The value passed to the echo statement, is actually an SimpleXML Object. The echo requests the objects 'string equivalent' when printing, and that is why it looks good. But if you want to do any comparisons, you should force the object to a string (or integer) like so: <?php ... $district = (string)$xml->channel->item->children($ddb_ns)->district; if($district == 'SD'){ echo "Found SD"; } ?> Does that make sense? Quote Link to comment Share on other sites More sharing options...
dfowler Posted February 4, 2008 Author Share Posted February 4, 2008 The ddb: registers values in a particular 'namespace'. To access those values, you need to get the children for that particular namespace: <?php $url = 'http://site.com/publish/davis/info/abc.rss'; $xml = simplexml_load_file($url); $ddb_ns = 'http://site.com/schema'; echo $xml->channel->item->children($ddb_ns)->male; ?> Also, just a note. The value passed to the echo statement, is actually an SimpleXML Object. The echo requests the objects 'string equivalent' when printing, and that is why it looks good. But if you want to do any comparisons, you should force the object to a string (or integer) like so: <?php ... $district = (string)$xml->channel->item->children($ddb_ns)->district; if($district == 'SD'){ echo "Found SD"; } ?> Does that make sense? Ok, that makes things display now. I "think" I'm starting to get it, but it is all a tad bit confusing. I'm going to keep punching away at it now. Thanks for you help so far! I will probably chime back in with some questions down the road. Quote Link to comment Share on other sites More sharing options...
dfowler Posted February 4, 2008 Author Share Posted February 4, 2008 Ok, found my first problem. I was running all this locally on WAMP server to test it, however, after uploading it I realized a major problem. The server is currently running PHP4. The SimpleXML only works on PHP5. Does anybody know a way to get this to work with php4? <?php error_reporting(E_ALL) ; ini_set('display_errors','1'); $url = 'http://site.com/publish/davis/info/abc.rss'; $xml = simplexml_load_file($url); $ddb_ns = 'http://site.com/schema'; $SD=0; $HD=0; foreach ($xml->channel->item as $item) { $district = $item->children($ddb_ns)->district; if($district == 'SD'){ $SD += 1; } else { $HD += 1; } } echo "South: ".$SD."<br />"; echo "Harbor: ".$HD; ?> Quote Link to comment Share on other sites More sharing options...
rhodesa Posted February 4, 2008 Share Posted February 4, 2008 Personally, I would try to find a way to get PHP5 on your server. Some hosting services allow you to turn on PHP5 in a control panel or with an .htaccess fine. Otherwise, you are stuck with these options: -http://us.php.net/manual/en/ref.domxml.php (not very fun) -Parsing the data manually (also not fun) Quote Link to comment Share on other sites More sharing options...
dfowler Posted February 4, 2008 Author Share Posted February 4, 2008 Personally, I would try to find a way to get PHP5 on your server. Some hosting services allow you to turn on PHP5 in a control panel or with an .htaccess fine. Otherwise, you are stuck with these options: -http://us.php.net/manual/en/ref.domxml.php (not very fun) -Parsing the data manually (also not fun) Well here is the problem with that. My friend needs it done by tomorrow at noon. After what you've showed me, I would have no problem finishing all he wants in a couple of hours. However, I called the hosting provider and they said it would take at least a day to switch over to PHP5. So it appears I'm stuck doing this in PHP4. Time to learn something new all over again I guess. Quote Link to comment Share on other sites More sharing options...
rhodesa Posted February 4, 2008 Share Posted February 4, 2008 There are also backports of SimpleXML out there, don't have a recommendation on which one to use though. Here is one I found with a Google Search real quick: http://www.ister.org/code/simplexml44/index.html Quote Link to comment Share on other sites More sharing options...
dfowler Posted February 5, 2008 Author Share Posted February 5, 2008 Ok, I got it up on the server. I've now run into a new problem. This code works great: foreach ($xml->channel->item as $item) { $district = $item->children($ddb_ns)->district; if($district == 'SD'){ if($item->children($ddb_ns)->male == 1){ However, this code doesn't work at all: foreach ($xml->channel->item as $item) { $district = $item->children($ddb_ns)->district; $year = $item->children($ddb_ns)->year; if($year = 2012) { if($district == 'SD'){ if($item->children($ddb_ns)->male == 1){ I tried a print_r($term); and came up with this: SimpleXMLElement Object ( [0] => 2,020 ) SimpleXMLElement Object ( [0] => 2,016 ) SimpleXMLElement Object ( [0] => 2,016 ) SimpleXMLElement Object ( [0] => 2,016 ) SimpleXMLElement Object ( [0] => 2,016 ) SimpleXMLElement Object ( [0] => 2,020 ) SimpleXMLElement Object ( [0] => 2,016 ) SimpleXMLElement Object ( [0] => 2,020 ) SimpleXMLElement Object ( [0] => 2,020 ) SimpleXMLElement Object ( [0] => 2,012 ) SimpleXMLElement Object ( [0] => 2,016 ) ect.... However, when I try any if statement including: if($term == 2012){ or if($term == '2012'){ It breaks? What am I doing wrong Quote Link to comment Share on other sites More sharing options...
rhodesa Posted February 5, 2008 Share Posted February 5, 2008 I don't see a 'year' element in your RSS xml. Also, where is $term coming from? Quote Link to comment Share on other sites More sharing options...
dfowler Posted February 5, 2008 Author Share Posted February 5, 2008 Yeah, I feel dumb. I was crossing two columns in the rss feed. Everything works perfectly now, thanks for all your help! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.