SitePoint Sponsor

User Tag List

Results 1 to 2 of 2

Thread: RSS To MYSQL ->

  1. #1
    SitePoint Enthusiast drownedbeaver's Avatar
    Join Date
    Aug 2011
    Location
    Philippines
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question RSS To MYSQL ->

    I got a feed, and use xml_parser_create() to manipulate it.... Then How do I save it to the database?

    I am ok with these tutorial. So what I need is to save the individual chunk of information to the database.

    thanks guys

  2. #2
    SitePoint Enthusiast drownedbeaver's Avatar
    Join Date
    Aug 2011
    Location
    Philippines
    Posts
    39
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Lightbulb

    I got a hand from my friends offline and they have this RSS extractor thingy
    they said I can share it to the world so here it is.

    Code:
    CLASS rss_parser { 
     
    // constructor. setup parser options and handlers. 
    FUNCTION rss_parser() { 
      $this->error = ''; 
      $this->file = ''; 
     
      $this->channel = ARRAY(); 
      $this->data = ''; 
      $this->stack = ARRAY(); 
      $this->num_items = 0; 
     
      $this->xml_parser = XML_PARSER_CREATE(); 
      XML_SET_ELEMENT_HANDLER($this->xml_parser, "rss_start_element", "rss_end_element"); 
      XML_SET_CHARACTER_DATA_HANDLER($this->xml_parser, "rss_character_data"); 
    } 
     
    FUNCTION character_data($parser, $data) { 
      IF (EMPTY($this->data)) $this->data = TRIM($data); // concatenate non-parsed data... 
      ELSE $this->data .= ' '.TRIM($data);               // and get rid of white space. 
    } 
     
    FUNCTION start_element($parser, $name, $attrs) { 
      SWITCH($name) { 
        CASE 'RSS': 
          BREAK; 
     
        CASE 'CHANNEL': 
          BREAK; 
     
        CASE 'IMAGE': 
          ARRAY_PUSH($this->stack, $name); 
          BREAK; 
     
        CASE 'ITEM': 
          ARRAY_PUSH($this->stack, $name); 
          ARRAY_PUSH($this->stack, $this->num_items); // push item index. 
          $this->item[$this->num_items] = ARRAY(); 
          $this->num_items++; 
          BREAK; 
     
        CASE 'TEXTINPUT': 
          ARRAY_PUSH($this->stack, $name); 
          BREAK; 
     
        DEFAULT: 
          ARRAY_PUSH($this->stack, $name); 
          BREAK; 
     
      }   
    } 
     
    FUNCTION end_element($parser, $name) { 
      SWITCH ($name) { 
        CASE 'RSS': 
          BREAK; 
     
        CASE 'CHANNEL': 
          BREAK; 
     
        CASE 'IMAGE': 
          ARRAY_POP($this->stack); 
          BREAK; 
     
        CASE 'ITEM': 
          ARRAY_POP($this->stack); 
          ARRAY_POP($this->stack); 
          BREAK; 
     
        CASE 'TEXTINPUT': 
          ARRAY_POP($this->stack); 
          BREAK; 
     
        DEFAULT: // child element. 
          $element = (IMPLODE("']['",$this->stack));      
          EVAL("\$this->channel['$element']=\$this->data;"); // this does all the hard work. 
          ARRAY_POP($this->stack); 
          $this->data = ''; 
          BREAK; 
      } 
    }
     
     
    FUNCTION parse() { 
      IF (!($fp = @FOPEN($this->file, "r"))) { 
        $this->error = "Could not open RSS source \"$this->file\"."; 
        RETURN FALSE; 
      } 
      WHILE ($data = FREAD($fp, 4096)) { 
        IF (!XML_PARSE($this->xml_parser, $data, FEOF($fp))) { 
          $this->error = SPRINTF("XML error: %s at line %d.", 
            XML_ERROR_STRING(XML_GET_ERROR_CODE($this->xml_parser)), 
            XML_GET_CURRENT_LINE_NUMBER($this->xml_parser)); 
          RETURN FALSE; 
        } 
      } 
      XML_PARSER_FREE($this->xml_parser); 
      RETURN TRUE; 
    } 
    }
    This is made out of PHP4 it is working you can all feast with it.
    Can you just help me convert this to a more readable approach.... but its alien to me.
    Actually the flow of the start_element is the one that bugs me..... xml_parser I understand but in
    some parts it just bleed my nose out.

    Thanks guys!


Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •