I am trying to write a class to use with the SearchHippo search engine that will make it much easier to use.

I am trying to make it just ONE class, with none of it's functionality contained outside of that class.

My problem is this :

xml_set_element_handler ($this->xml_parser,
"this->startElementHandler",
"this->endElementHandler");

But apparently, you cannot use the '$this->' reference within the
parameters of xml_set_element_handler

This is a problem, because I need to have all of the state info
set INSIDE the class... if anybody has any answers or suggestions, I would GREATLY appreciate the help!!!!!

    $local_parser = $this->xml_parser;
    $local_seh    = $this->startElementHandler;
    $local_eeh    = $this->endElementHangler;
    xml_set_element_handler ($local_parser,$local_seh,$local_eeh");
    

      I already tried that....
      I just get this message for every time the handler is supposed to be called...

      Warning: xml_parse(): Unable to call handler () in C:\Program Files\Apache Group\Apache2\htdocs\hippo.php on line 90

      Any other ideas???

        Look through the comments at the end of the OOP chapter in the manual. You will find that it's possible to pass an array containing an object reference and a method name.

        xml_set_element_handler (array($this,'xml_parser'),...

          AHHHH!!! It works!!! That's a really strange solution, but it shows off that limitations can be overcome by the inherent flexibility of PHP (sounds like an advertisement, huh?)
          Thanks a whole bunch!
          (I'm not going to mark this resolved for another day or so... just in case --- but you people are the best!)😃 😃 😃

            It doesn't give me any errors anymore... but it doesn't work either! Can someone tell me if there's something wrong with my code? It's base on the code provided by SearchHippo.com, and I am making an OOP module out of it (which I am going to give to SearchHippo, so everyone can use it).

            Pleeeeeease.... I'm actually losing sleep over this. 🙁

            class Hippo {
              var $recctr;
              var $curstate;
              var $recs;
              var $qrystatus;
              var $data_version;
              var $in_item_tag;
              var $xml_parser;
              var $q;
              var $i;
              var $c;
              var $qenc, $ienc, $cenc;
            
            function startElementHandler ($parser, $element_name, $element_attribs)
            {
              if ($element_name == "RESULTS") {
                $this->qrystatus ["count"] = trim ($element_attribs ["COUNT"]);
                $this->qrystatus ["head"] = trim ($element_attribs ["HEAD"]);
                $this->qrystatus ["tail"] = trim ($element_attribs ["TAIL"]);
                $this->qrystatus ["nexturl"] = trim ($element_attribs ["NEXTURL"]);
                $this->qrystatus ["prevurl"] = trim ($element_attribs ["PREVURL"]);
              }
            
              if ($element_name == "RECORD") {
                $this->recs [$this->recctr] ["id"] = trim ($element_attribs ["ID"]);
                $this->recs [$this->recctr] ["timestamp"] = trim ($element_attribs ["TIMESTAMP"]);
                $this->recs [$this->recctr] ["size"] = trim ($element_attribs ["SIZE"]);
              }
            
              if ($element_name == "SEARCHHIPPO") {
                $this->data_version = trim ($element_attribs ["VERSION"]);
              }
            
              $this->curstate = $element_name;
            }
            
            function endElementHandler ($parser, $element_name)
            {
              $this->curstate = "";
            
              if ($element_name == "RECORD") { $this->recctr++; }
            }  
            
            function characterDataHandler ($parser, $data)
            {
              if ($this->curstate == "") return;
              else if ($this->curstate == "URL") { $this->recs [$this->recctr] ["url"] = trim ($data); }
              else if ($this->curstate == "DISPURL") { $this->recs [$this->recctr] ["dispurl"] = trim ($data); }
              else if ($this->curstate == "TITLE") { $this->recs [$this->recctr] ["title"] = trim ($data); }
              else if ($this->curstate == "DESCR") { $this->recs [$this->recctr] ["descr"] = trim ($data); }
            }
            
            function Hippo() {
              $this->recctr = 0;             // number of records retrieved
              $this->in_item_tag = 0;        // flag for expat parsing
              $this->curstate = '';          // for xpath state
              $this->recs = array ();        // returned data results
              $this->qrystatus = array ();   // returned data status
              $this->data_version = '';      // version of results page
              $this->xml_parser = xml_parser_create();
              //credit for this "fix" goes to ahundiak of PHPBuilder.com
              xml_set_element_handler ($this->xml_parser,array($this,startElementHandler),array($this,endElementHandler)); 
              xml_set_character_data_handler ($this->xml_parser,array($this,characterDataHandler));
              //--------------
              $this->q = $_GET['q'];
              $this->q = stripslashes($this->q);
              $this->i = $_GET['i'];
              $this->c = $_GET['c'];
              $this->qenc = urlencode($this->q);
              $this->ienc = urlencode($this->i);
              $this->cenc = urlencode($this->c);
              $qenc = $this->qenc;
              $ienc = $this->ienc;
              $cenc = $this->cenc;
            
              $fp = fopen (
                  "http://www.searchhippo.com/qxml.php?q=$qenc&c=$cenc&i=$ienc", "r");
              if ($fp) {
                $data = fread ($fp, 4096);
                while ($data) {
            	  //if (!xml_parse ($this->xml_parser, $data, feof ($fp))) { break; }
                  xml_parse($this->xml_parser,$data,feof($fp));
            	  $data = fread ($fp, 4096);
                }
              }
              xml_parser_free ($this->xml_parser);
              // This should output some of the parse data... but it's
              // always empty!!!
              echo $this->recctr;
              print_r($this->recs);
              print_r($this->qrystatus);
              }
            }
            
            $search = new Hippo();
            

              what im using:

              xml_set_object($xml_parser,&$this);
              

              why &$this is used:

              Since xml_set_element_handler and xml_set_character_data_handler cannot take references to the methods of objects as the function names of the event handlers (i.e. xml_set_element_handler($xml_parser,"$rss_parser->startElement",... won't work), you need a way to tell the XML parser to call methods of the $rss_parser object instead of basic functions. xml_set_object does just that, taking the parser as well as a reference (note the &) to the object whose methods you want it to call.

              for more:
              http://www.sitepoint.com/article/560/4

                That works perfectly!!! I appreciate it very much!!!
                I hope that I actually have answers for some other people at some point to repay all of the help you've given me!

                  Write a Reply...