Page 1 of 1

RSS/XML Parser - Woe is me

Posted: Tue Jan 17, 2006 12:06 pm
by lemonfreshmedia
Well, having more troubles with this parser again.
It seems no one uses it for more than getting 'item' ( or one node deep) from an rss/xml feed.

I have an xml file with sports drafts.

the format is like this.

Code: Select all

<NHLDRAFTS>

     <NHL2005>

          <ROUND1>
                            </NAME>
                             </PLAYER>
                              </TEAM>
          </ROUND1>
           <ROUND1>
                            </NAME>
                             </PLAYER>
                              </TEAM>
          </ROUND1>
          <ROUND2>
                            </NAME>
                             </PLAYER>
                              </TEAM>
          </ROUND2>
          <ROUND2>
                            </NAME>
                             </PLAYER>
                              </TEAM>
          </ROUND2>

     </NHL2005>

</NHLDRAFTS>
Can someone please help with parsing for a specific round for a specific year?

so i'd like to get all the picks for

NHLDRAFTS /NHL2005/ ROUND1 etc...

here's what I've been playing with

It's returning all ROUND2 PICKS including all other years all the way to the mid 90's!


Code: Select all

<?php

class RSSParserCOL1 {
var $yeartest = 'NHL2005';
var $roundtest = 'ROUND2';

   /* The number of items to retrieve */
  var $maxitems = 500;
  
  var $insideitem = 'NHL2005';
  var $tag = "";
  var $title = "";
  var $description = "";
  var $link = "";
  
  var $none = "nolink";
  var $mycounter = "fakdjhfjkadhjklas";

function startElement($parser, $tagName, $attrs) {
  if ($this->insideitem)  {
    $this->tag = $tagName;
  } elseif ($tagName == ($this->roundtest)) {
    $this->insideitem = true;
  }
}
 function endElement($parser, $tagName) {
  if ($tagName == ($this->roundtest) ) {
    printf("
   <div id=\"pick-odd\">	
	
	
  <div id=\"logo\"><img width=\"60\" height=\"60\"src=");
    printf("\"NHL_50/%s\"></div><div id=\"pick-details\">",($this->draftedby));
	printf("<div id=\"draft-number\">Draft#:%s</div>",($this->draftnumber));
	
	printf("<div id=\"player\">%s</div>",($this->player));
	printf("Position:%s<br>",($this->position));
	printf("Drafted From:%s<br></div> 
</div>",($this->draftedfrom));

	$this->draftedfrom = "";
	$this->draftnumber = "";
    $this->position = "";
    $this->draftedby = "";
    $this->player = "";
    $this->insideitem = false;
  }
}



function characterData($parser, $data) {
  if ($this->insideitem) {
    switch ($this->tag) {
      case "DRAFTNUMBER":
        $this->draftnumber .= $data;
        break;
      case "POSITION":
        $this->position .= $data;
        break;
		case "DRAFTEDFROM":
        $this->draftedfrom .= $data;
        break;
      case "DRAFTEDBY":
        $this->draftedby .= $data;
        break;
      case "PLAYER":
        $this->player .= $data;
        break;
    }
  }
}
}

$xml_parser = xml_parser_create();
$rss_parser = new RSSParserCOL1();

xml_set_object($xml_parser,&$rss_parser);
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");

$fp = fopen("http://nhl_drafts_.xml","r")
  or die("Error reading RSS data.");

/* Reads in all the data at once
   into a temporary variable */
while ($data = fread($fp, 4096)) {
  $tmp .= $data;
}

/* Splits up (tmp) into items */
$items = split('</round2>', $tmp);

/* Cycles thru (items) with (maxitems) iterations */
for ($i = 0; $i < $rss_parser->maxitems; $i++) {
  $xml .= $items[$i].'</round2>';
}
$xml .= $items[sizeof($items)-1]; //<-- The remaining XML

xml_parse($xml_parser, $xml);

fclose($fp);
xml_parser_free($xml_parser);
$xml = null;
$data =null;
$items =null;
$tmp = null;
?>

Can anyone help me with this? I'll select you in my Draft for PHPdN member of the year!
Thanks again all for any help you can provide.

Correction

Posted: Tue Jan 17, 2006 12:23 pm
by lemonfreshmedia
Sry here's the code that is working for me returning all the 'rounds'

Code: Select all

<?php

class RSSParserCOL2 {
var $roundtest = 'ROUND2';

  /* The number of items to retrieve */
  var $maxitems = 500;
  
  var $insideitem = false;
  var $tag = "";
  var $title = "";
  var $description = "";
  var $link = "";
  
  var $none = "nolink";
  var $mycounter = "fakdjhfjkadhjklas";

function startElement($parser, $tagName, $attrs) {
  if ($this->insideitem) {
    $this->tag = $tagName;
  } elseif ($tagName == ($this->roundtest)) {
    $this->insideitem = true;
  }
}
 function endElement($parser, $tagName) {
  if ($tagName == ($this->roundtest) ) {
    printf("
   <div id=\"pick-odd\">	
	
	
  <div id=\"logo\"><img width=\"60\" height=\"60\"src=");
    printf("\"NHL_50/%s\"></div><div id=\"pick-details\">",($this->draftedby));
	printf("<div id=\"draft-number\">Draft#:%s</div>",($this->draftnumber));
	
	printf("<div id=\"player\">%s</div>",($this->player));
	printf("Position:%s<br>",($this->position));
	printf("Drafted From:%s<br></div> 
</div>",($this->draftedfrom));

	$this->draftedfrom = "";
	$this->draftnumber = "";
    $this->position = "";
    $this->draftedby = "";
    $this->player = "";
    $this->insideitem = false;
  }
}



function characterData($parser, $data) {
  if ($this->insideitem) {
    switch ($this->tag) {
      case "DRAFTNUMBER":
        $this->draftnumber .= $data;
        break;
      case "POSITION":
        $this->position .= $data;
        break;
		case "DRAFTEDFROM":
        $this->draftedfrom .= $data;
        break;
      case "DRAFTEDBY":
        $this->draftedby .= $data;
        break;
      case "PLAYER":
        $this->player .= $data;
        break;
    }
  }
}
}

$xml_parser = xml_parser_create();
$rss_parser = new RSSParserCOL2();

xml_set_object($xml_parser,&$rss_parser);
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");

$fp = fopen("http://24.244.141.135/alpha/sc/draft/custom_feeds/rewrite/nhl_drafts_col2.xml","r")
  or die("Error reading RSS data.");

/* Reads in all the data at once
   into a temporary variable */
while ($data = fread($fp, 4096)) {
  $tmp .= $data;
}

/* Splits up (tmp) into items */
$items = split('</round2>', $tmp);

/* Cycles thru (items) with (maxitems) iterations */
for ($i = 0; $i < $rss_parser->maxitems; $i++) {
  $xml .= $items[$i].'</round2>';
}
$xml .= $items[sizeof($items)-1]; //<-- The remaining XML

xml_parse($xml_parser, $xml);

fclose($fp);
xml_parser_free($xml_parser);
$xml = null;
$data =null;
$items =null;
$tmp = null;
?>