Re: Expat Wrapper and SAX Filters

From: Date: Wed, 10 Apr 2002 21:03:29 +0000
Subject: Re: Expat Wrapper and SAX Filters
References: 1 2 3  Groups: php.pear.dev 
Request: Send a blank email to pear-dev+get-5318@lists.php.net to get a copy of this message
I can't make any promises on APIs that are not yet written, but what I have in mind is this: $xp = new XML_Parser(...); $xp->addListener($this); During parsing, the same methods will be called in $this as in $xp. It would actually make the methods in $xp optional too, and you can add more listeners if you want. - Stig On Wed, 2002-04-10 at 15:21, Luis Argerich wrote: > Stig, > > Can you give me some ideas about what the interface may look like some usage > example or anything > that can be useful? I'm working on a PHP book and we'll comment the > XML_Parser class and I think > it would be very nice to comment how you'll be able to code SAX filters and > chains of filters with the > listener interface even if this is not done yet. > > If you have n o clue no problem. > > Thanks, > Luis > > "Stig S. Bakken" <ssb@fast.no> escribió en el mensaje > news:1018436169.18496.2.camel@kirin.trondheim.fast.no... > > Unless someone beats me to it, I'll implement a listener interface in > > the next XML_Parser release. > > > > - Stig > > > > On Tue, 2002-04-09 at 19:14, Luis Argerich wrote: > > > After taking a look at your Parser class wrapping the Expat parser I > wanted > > > to share and idea with you. > > > > > > IMHO the SAX wrapper class should allow the user to set a "listener" > object, > > > an object where SAX events are passed, > > > the SAX wrapper should implement all the SAX handlers and only do a > > > $this->listener->nameOfHandler(params) inside. > > > This way we can program SAX filters that receive SAX events and produce > more > > > SAX events or some result, chaining > > > filters allow to build complex SAX transformations/modifications or > other > > > operations over XML documents. > > > > > > For example take a look at this abstract classes: > > > > > > class AbstractSAXParser { > > > var $listener; > > > function AbstractSAXParser() {} > > > function parserSetoption($opt,$val) {} > > > function setListener($obj) { > > > $this->listener=$obj; > > > } > > > function startElementHandler($parser,$name,$attribs) { > > > $this->listener->startElementHandler($name,$attribs); > > > } > > > function endElementHandler($parser,$name) { > > > $this->listener->endElementHandler($name); > > > } > > > function characterDataHandler($parser,$data) { > > > $this->listener->characterDataHandler($data); > > > } > > > function parse() {} > > > } > > > > > > class AbstractFilter { > > > var $listener; > > > > > > function setListener($obj) { > > > $this->listener=$obj; > > > } > > > function startElementHandler($name,$attribs) {} > > > function endElementHandler($name) {} > > > function characterDataHandler($data) {} > > > > > > } > > > > > > The abstract SAX parser wraps a SAX parser, we can make a derivative > class > > > for Expat or we can also derivate the AbstractClass to a class > > > that parses non-xml data generating SAX events and thus allowing to > convert > > > non-xml data to XML. > > > > > > The Filter is the class that receives SAX events and in the handlers > perform > > > some operations and may propagate the events to a next listener, > > > a final Filter that outputs XML for example just intercepts the events > but > > > don't propagate them. > > > > > > Example: > > > > > > $parser=new ExpatParser($xmlfilename); // ExpatParser extends > > > AbstractSAXPaser > > > $filter=new Filter(); // Some filter > > > $parser->setListener($filter); > > > $parser->parse(); > > > We can for example chain filters to transform a document using several > minor > > > "modular" transformations or modify a document using a filter > > > that adds or remove elements (several elements can be added or removed > if we > > > queue the modifications in the filter before parsing) > > > This leads to several advantages since complex > transformations/modifications > > > can be performed reading the XML file just once and at > > > SAX speed and cosuming very few resources. Results can also be streammed > > > since SAX allows streaming the result as it is generated. > > > > > > I believe that PEAR is the place where the "standard" AbstractSAXParser > and > > > Filter classes can be defined standarizing the way to > > > write SAX filters with PHP. > > > > > > Let me know what you think about this. > > > > > > > > > > > > > > > > > > -- > > > PEAR Development Mailing List (http://pear.php.net/) > > > To unsubscribe, visit: http://www.php.net/unsub.php > > > > > > -- > PEAR Development Mailing List (http://pear.php.net/) > To unsubscribe, visit: http://www.php.net/unsub.php

« previous php.pear.dev (#5318) next »