PHP XML Classes |
Sax Filters (class_sax_filters.php) |
Description: This is a set of classes implementing SAX filters, the classes include a SAX class to parse XML documents using Expat and defines a way to create SAX filters to perform SAX- based queries, updates and transformations of documents. Simple filters can be chained to construct augmented complex XML processors. Interfaces for filters are very easy to define. The classes include a mechanism to stream the output of "final" filters in order to increase efficiency. |
NEWS:
|
This class code as well as documentation are hosted at SourceForge please visit our SourceForge page for releases, documentation, bug-tracking, support forums and mailing lists. |
Resources | Requirements |
|
Features | To-dos |
|
|
Contact: Luis Argerich (lrargerich@yahoo.com) |
Detailed description and usage:
This is an overview of the classes that this package defines and how to use each one class AbstractSAXParser: This class defines an abstract SAX parser, if you want to build your own SAX parser or adapt some parser you should implement this class methods. What does a SAX parser do? It must parse the XML document and generate "events" that are passed to a listener object. Note that the parser doesn't process the XML document at all it just parses the documents and generate events that will be processed by a listener object (An AbstractFilter object). Class methods to be implemented are:
TIP:Note that you can implement an AbstractSAXParser for non-xml data converting the non-XML data to XML by simply producing events and then processing the events using filters that are prepared for XML processing. class ExpatParser: This class is an implementation of the AbstractSAXParser class using the PHP built-in expat parser which is, precicesly, a SAX parser. This version receives the XML as a file receiving the name of the file as an argument of the constructor. The class can be used as following: $parser = new ExpatParser("foo1.xml"); $filter=new SomeFilterHere(); $parser->setListener($filter); $parser->parse(); Note that all the processing is done at the filter so it is time to see what a filter is (keeo reading, it's easy!) class AbstractFilter:Filters are objects that receive SAX events (from a parser or another filter), process them doing something useful and then pass the events to another filter or output the result in some way, filters that don't propagate events are called "finalizer" filters and are typically filters that output the document to the browser or a file. Filters must extend the AbstractFilter class implementing the following methods:
Besides that methods all filters have a predefined "setListener" method that allows you to set a listener object for the filter events what is needed to propagate events from one filter to another. As an example two filters are provided in the package: FilterName and FilterNameBold, FilterName converts all the <name>something</name> elements uppercasin its content for example to <name>SOMETHING</name> The FilterNameBold adds a "bold" element to all name elements thus converting <name>something</name> into <name><b>something</b></name> The FilterOutput method is a "finalizer" filter that doesn't propagate events, it just outputs the XML content to the browser. So it is useful as the last filter in filter chains for testing. If you want to convert all name elements to uppercase you use the classes as follows: include_once("class_sax_filters.php"); $f1=new ExpatParser("applications.xml"); $f1->parserSetOption(XML_OPTION_CASE_FOLDING,0); $f2=new FilterName(); $f3=new FilterOutput(); $f2->setListener($f3); $f1->setListener($f2); $f1->parse(); We create an Expat parser, a FilterName object and a FilterOutput object. First we set the FilterOutput as the FilterName listener, what means that events created by FilterName will be passed to FilterOutput. Then we set the FilterName as the parser listener what means that events generated at the parser level will be propagated to FilterName and since FilterName passes events to FilterOutput that will be the last link in the filter chain. The order in which listeners are set is very important since when we set the parser listener that object must already have been set with a listener in order to do something. Then we just call the parse method. What will happen is that the parser will parse the XML document generating events, the events will be passed to filterName where name elements are uppercased and then the events will be propagated to filterOutput where the content is just printed. Filter Chains can be as complex as you want linking several filters to produce a complex task. Filters can add elements, remove elements (absorbing events) and change elements thus allowing any kind of XML processing from queries to transformations. SAX filters are a sound way to modularize SAX processing of XML documents. When documents are very large or huge only a SAX based processing is efficient since SAX never reads the whole document in memory it just processes the document chunk by chunk. DocumentationClassesAbstractSAXParserExtends: NoneDescription: This is an abstract class defininf the methods that SAX parsers must implement in order to be able to work with SAX filters.
AbstractSAXParservoid AbstractSAXParser()
parsevoid parse()
setListenervoid setListener(Object $obj)
AbstractFilterExtends: NoneDescription: This class defines the methods that must be implemented by a Filter.
setListenervoid setListener(object $obj)
startElementvoid startElement(string $name, array $attribs)
endElementvoid endElement(string $name)
characterDataHandlervoid characterDataHandler(string $data)
ExpatParserExtends: AbstractSAXParserDescription: This is an implementation of the AbstractSAXParser class using the PHP internal Expat parser
ExpatParservoid ExpatParser(string $xmlfile)
parsevoid parse(string $xmlfile)
setListenervoid setListener(object $obj)
parserSetOptionvoid parserSetOption(constant $option, some $value)
FilterOutputExtends: AbstractFilterDescription: This is a finalizer filter that must be used always at the end of a filter chain. This filter absorbs SAX events ouputting the XML document to the browser.
| |||||||||||||||||||||||||||||||||||||||||||||||||||
This class doesn´t have any method |