Jump to content

[SOLVED] RSS Question


dfowler

Recommended Posts

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>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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?

 

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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;
?>

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

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

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.