Connecting Tech Pros Worldwide Forums | Help | Site Map

expat XML Parsing in PHP

Newbie
 
Join Date: May 2009
Posts: 1
#1: May 14 '09
Im trying to parse the following

Expand|Select|Wrap|Line Numbers
  1. <channel>
  2. <newsItem>
  3. <newsTitle>U.S. Tops Guatemala 2-0 in Cup Qualifier (AP)</newsTitle>
  4.  <reporters>
  5.     <reporter>RONALD BLUM</reporter>
  6.  </reporters>
  7.   <ratings>
  8.   <rating>3</rating>
  9.   <rating>2</rating>
  10.   <rating>2</rating>
  11.   <rating>5</rating>
  12.   <rating>3</rating>
  13.   <rating>5</rating>
  14.   <rating>3</rating>
  15.   <rating>4</rating>
  16.  </ratings>
  17. <pubDate>Thu, 31 Mar 2005 03:29:44 GMT</pubDate>
  18. </newsItem>
  19. <!-- more newsItems with similar structure -->
Im trying to parse EACH newsItem and show
-Article Title (newsTitle)
-Reporter (reporter)
-Date (pubDate)

I CANNOT DO THIS PART BELOW
-# Of ratings (count # of ratings in rating array)
-avg ratings (sum of ratings in array / count )
-max rating ( max / highest rating)

Code is below

Expand|Select|Wrap|Line Numbers
  1. <?php
  2.   $g_channel = array();
  3.   $g_elem = null;
  4.  
  5.   function startElement( $parser, $name, $attrs ) 
  6.   {
  7.  
  8.   global $g_channel, $g_elem;
  9.   if ( $name == 'newsItem' ) $g_channel []= array();
  10.   $g_elem = $name;
  11.   }
  12.  
  13.   function endElement( $parser, $name ) 
  14.   {
  15.   global $g_elem;
  16.   $g_elem = null;
  17.   }
  18.  
  19.   function textData( $parser, $text )
  20.   {
  21.  
  22.   global $g_channel, $g_elem;
  23.   if ( $g_elem == 'REPORTER' ||
  24.   $g_elem == 'PUBDATE' ||
  25.   $g_elem == 'NEWSTITLE' )
  26.   {
  27.   $g_channel[ count( $g_channel ) - 1 ][ $g_elem ] = $text;
  28.   }
  29.   }
  30.  
  31.   $parser = xml_parser_create();
  32.  
  33.   xml_set_element_handler( $parser, "startElement", "endElement" );
  34.   xml_set_character_data_handler( $parser, "textData" );
  35.  
  36.   $f = fopen( 'yahoosports.xml', 'r' );
  37.  
  38.   while( $data = fread( $f, 4096 ) )
  39.   {
  40.   xml_parse( $parser, $data );
  41.   }
  42.  
  43.   xml_parser_free( $parser );
  44.  
  45.  
  46.   foreach( $g_channel as $newsItem )
  47.   {
  48.  
  49.   echo $newsItem['NEWSTITLE']." - ".$newsItem['REPORTER']." - ";
  50.   echo $newsItem['PUBDATE']."\n";
  51.  
  52.   }
  53.  
  54.   ?>
MUST BE DONE IN EXPAT AND NOT DOM!

Moderator
 
Join Date: Mar 2006
Posts: 1,104
#2: May 14 '09

re: expat XML Parsing in PHP


Assuming your ratings are per newsItem. Create 3 variables, and change your text handler.
Expand|Select|Wrap|Line Numbers
  1. # reset the variables at the start of every newsItem. 
  2. $rating_count = 0
  3. $rating_total = 0
  4. $rating_max = -1
  5.  
  6.  
  7. in your text handler,
  8.  
  9. else if ( $g_elem == 'rating'){
  10.   $rating_count++;
  11.   $rating_total += $text;
  12.   if ($text > $rating_max){
  13.      $rating_max = $text;
  14.    }
  15.  
On the appropriate end element, output the data: $rating_total / $rating_count = rating average. Don't forget a check, if $rating_total = 0, for no ratings.
Reply