Re: References & Object XML-Parser

From: Date: Wed, 16 May 2001 20:50:34 +0000
Subject: Re: References & Object XML-Parser
References: 1  Groups: php.general 
Request: Send a blank email to php-general+get-53101@lists.php.net to get a copy of this message
I am new to PHP's OO (though not to OO or to PHP), but here's a fix and my assessment of why it works: In your tag functions, you need to change the $this reference to instead refer to your $xml_parser object, thus: function tag_open($parser,$tag,$attributes) { global $xml_parser; $xml_parser->result .= "TAG: ".$tag."<br>"; } My speculation as to why this happens is that the actual parsing is going on in the parser object you create in the xml() function. The tag_open function is being called not as a method in your xml class but rather as a simple callback function, so it does not get all the context of the xml object... instead, it has the context of the parser object. So if you bring the $xml_parser variable in as a global and then use it to set your result variable, you should see the results you're looking for (I did). Pete. "Mirek Novak" <mirek.novak@auto.cz> wrote in message news:3B028148.CA91176D@auto.cz... > Hi, > > I'm new to this list, and was forced to join because of following > problem, BTW searching through the archive ended without any useable > results. The problem is in following code - which is little extended > source from example in manual entry for xml_set_object() function. I've > added one member variable called result and there I'm storing everything > what is parsed through parser - I want to have control over what is > parsed - then, when I wanted to show the result - variable was empty, > but when u'll uncomments line in tag_open() function, will see > interesting results - var. is being updated. > Where is mistake? > > Mirek Novak > > ----------------------------------------- PHP code follows > ------------------------------------ > <? > class xml { > var $parser; > var $result; > > function xml() { > $this->parser = xml_parser_create(); > $this->result="<br><u>result</u><br>"; > xml_set_object($this->parser,&$this); > xml_set_element_handler($this->parser,"tag_open","tag_close"); > xml_set_character_data_handler($this->parser,"cdata"); > } > > function parse($data) { > xml_parse($this->parser,$data); > } > function tag_open($parser,$tag,$attributes) { > var_dump($parser,$tag,$attributes); > $this->result.="TAG: ".$tag."<br>"; > > // if u uncomment line below, U'll see, that $this->result really > contains what it shoud > // echo $this->result; > } > > function cdata($parser,$cdata) { > var_dump($parser,$cdata); > $this->result.="DATA: ".$cdata."<br>"; > } > > function tag_close($parser,$tag) { > var_dump($parser,$tag); > } > > } > > > $xml_parser = new xml(); > $xml_parser->parse("<A ID=\"hallo\">PHP</A>"); > echo $xml_parser->result; > > ?>

« previous php.general (#53101) next »