topical media & game development
server-php-xml-class-sax-filters-class-sax-filters.htm / htm
<html>
<head>
<title>Sax Filters (class_sax_filters.php)</title>
<link rel="stylesheet" href="main.css" type="text/css">
</head>
<body>
<div align="center">
<table border="0" width="80%" cellpadding="0" cellspacing="0">
<tr>
<td bgcolor="#aaaaee" class="text" align="center"><a href="http://phpxmlclasses.sourceforge.net/">PHP XML Classes</a></td>
</tr>
</table>
<table border="0" width="80%" cellpadding="0" cellspacing="0">
<tr>
<td bgcolor="#aaaaee" class="textblbl" align="center">Sax Filters (class_sax_filters.php)</td>
</tr>
</table>
<br/>
<table border="0" width="80%">
<tr>
<td bgcolor="#eeeeff" class="text">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.
</td>
</tr>
</table>
<br/>
<table border="0" bgcolor="ddddff" width="80%">
<tr>
<td valign="top" class="text" width="50%"><b>NEWS:</b>
<ul>
<li> 05-17-2002 First version of this set of classes released.
</ul>
</td>
<td valign="top" width="50%" class="text">
This class code as well as documentation are hosted at <a href="http://www.sourceforge.net">SourceForge</a>
please visit our <a href="http://www.sourceforge.net/projects/phpxmlclasses/">SourceForge page</a> for releases, documentation, bug-tracking, support
forums and mailing lists.
</td>
</tr>
</table>
<br/>
<table border="0" width="80%" cellpadding="0" cellspacing="0">
<tr>
<td class="textbl" bgcolor="aaaaee" width="50%">Resources</td>
<td class="textbl" bgcolor="aaaaee" width="50%">Requirements</td>
</tr>
</table>
<table border="1" width="80%" cellpadding="0" cellspacing="0">
<tr>
<td valign="top" bgcolor="eeeeff" class="text" width="50%">
<ul>
<li><a class="textbl" href="http://www.sourceforge.net/projects/phpxmlclasses">Downloads</a>
<li><a class="textbl" href="http://cvs.sourceforge.net/cgi-bin/viewcvs.cgi/phpxmlclasses/sax_filters/">Browse CVS</a>
<li><a class="textbl" href="http://sourceforge.net/tracker/?atid=470965&group_id=53613&func=browse">Report Bugs</a>
</ul>
</td>
<td valign="top" bgcolor="eeeeff" class="text" width="50%">
<ul>
<li>PHP 4.0.5+
</ul>
</td>
</tr>
</table>
<br/>
<table border="0" width="80%" cellpadding="0" cellspacing="0">
<tr>
<td class="textbl" bgcolor="aaaaee" width="50%">Features</td>
<td class="textbl" bgcolor="aaaaee" width="50%">To-dos</td>
</tr>
</table>
<table border="1" width="80%" cellpadding="0" cellspacing="0">
<tr>
<td valign="top" bgcolor="eeeeff" class="text" width="50%">
<ul>
<li> Arbietrary filters can be defined
<li> Filter chains can link any number of filters
<li> Easy to implement
</ul>
</td>
<td valign="top" bgcolor="ddddff" class="text" width="50%">
<ul>
<li> Support for processing instructions
</ul>
</td>
</tr>
</table>
<br/>
<table border="0" bgcolor="ddddff" width="80%">
<tr>
<!--
<td valign="top" class="text" width="50%"><b>To-do/future releases:</b>
<ul>
<li>Class
<li>Class
<li>Class
</ul>
</td> -->
<td valign="top" width="100%" class="text">
Contact: Luis Argerich (<a href="mailto:lrargerich@yahoo.com">lrargerich@yahoo.com</a>)
</td>
</tr>
</table>
<br/>
<table border="0" bgcolor="ddddff" width="80%">
<tr>
<!--
<td valign="top" class="text" width="50%"><b>To-do/future releases:</b>
<ul>
<li>Class
<li>Class
<li>Class
</ul>
</td> -->
<td valign="top" width="100%" class="text">
<b>Detailed description and usage:</b>
<br/><br/>This is an overview of the classes that this package defines and how to use
each one<br/><br/>
<b>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. <br/><br/>
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).<br/><br/>
Class methods to be implemented are:<br/><br/>
<table border="1">
<tr><td class="text">Method</td><td class="text">Description</td></tr>
<tr><td class="text">AbstractSAXParser()</td><td class="text">The constructor should build the parser and can receive an XML file, for example if needed indicating the XML document to be parsed. How the parser knows where is the document to parse is left free to the parser implementation.</td></tr>
<tr><td class="text">other optional methods</td><td class="text">Other optional methods allowinf to set XML documents from different sources, parser options etc can be added as needed.</td></tr>
<tr><td class="text">parse()</td><td class="text">This is the principal method of the class, parsers must parse the XML source and generate the proper events calling the following methods defined in this class: startElementHandler(name,parser,data) those methods will call the same methods on the listener object thus propagating SAX events to the listener as they are produced by the parser.</td></tr>
</table>
<br/>
<b>TIP:</b>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.
<br/><br/>
<b>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:<br/>
<pre>
filter=new SomeFilterHere();
filter);
name,name)</td><td class="text">This method is called when an element ends. It receives the name of the element.</td></tr>
<tr><td class="text">characterDataHandler(f1=new ExpatParser("applications.xml");
f2=new FilterName();
f2->setListener(f1->setListener(f1->parse();
</pre>
<br/>
We create an Expat parser, a FilterName object and a FilterOutput object.<br/><br/>
First we set the FilterOutput as the FilterName listener, what means that events created by FilterName
will be passed to FilterOutput.<br/><br/>
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.<br/><br/>
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.
<br/><br/>
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.
<br/><br/>
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.
<br/><br/>
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.
<br/>
<h3>Documentation</h3>
</div>
<h2>Classes</h2>
<ul>
<li><a href="#class23">AbstractSAXParser</a></li>
<li><a href="#class24">AbstractFilter</a></li>
<li><a href="#class25">ExpatParser</a></li>
<li><a href="#class26">FilterOutput</a></li>
</ul>
<a name="class23"></a>
<h3>AbstractSAXParser</h3>
Extends: None
Description: This is an abstract class defininf the methods that SAX parsers must implement in order to be able to work with SAX filters.
<br>
<!-- METHOD SUMMARY -->
<A NAME="pDomNode_method_summary"><!-- --></A>
<TABLE BORDER="1" CELLPADDING="3" CELLSPACING="0" WIDTH="100%">
<TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor">
<TD COLSPAN=2><FONT SIZE="+2">
<B>Method Summary</B></FONT></TD>
</TR>
<!-- A ROW -->
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE> void</CODE></FONT></TD>
<TD><CODE><B><A HREF="#class23method1">AbstractSAXParser</A></B>()</CODE>
<BR>
Constructor</TD>
</TR>
<!-- A ROW -->
<!-- A ROW -->
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE> void</CODE></FONT></TD>
<TD><CODE><B><A HREF="#class23method2">parse</A></B>()</CODE>
<BR>
Parses the XML source</TD>
</TR>
<!-- A ROW -->
<!-- A ROW -->
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE> void</CODE></FONT></TD>
<TD><CODE><B><A HREF="#class23method3">setListener</A></B>(Object parser,attribs), endElementHandler(name) and characterDataHandler(obj)</PRE>
<DL>
<DD>This method sets the listener to a parser object. The listener is a Filter object extending the AbstractFilter class that will receive the events generated by the parser and do something with them.<DD>
<DL>
<DT><B>Parameters:</B><DD>
obj)</CODE>
<BR>
Sets the Filter's listener objectname, array attribs)</CODE>
<BR>
Method that is called when an XML element starts</TD>
</TR>
<!-- A ROW -->
<!-- A ROW -->
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE> void</CODE></FONT></TD>
<TD><CODE><B><A HREF="#class24method3">endElement</A></B>(string data)</CODE>
<BR>
Method that will be called when text data is found</TD>
</TR>
<!-- A ROW -->
</TABLE>
<P>
<!-- METHOD SUMMARY -->
<!-- ============ METHOD DETAIL ========== -->
<A NAME="method_detail"><!-- --></A>
<TABLE BORDER="1" CELLPADDING="3" CELLSPACING="0" WIDTH="100%">
<TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor">
<TD COLSPAN=1><FONT SIZE="+2">
<B>Method Detail</B></FONT></TD>
</TR>
</TABLE>
<!-- method -->
<A NAME="class24method1"><!-- --></A>
<H3>setListener</H3>
<PRE>
void <B>setListener</B>(object obj - An object from a class extending the AbstractFilter class
name, array attribs)</PRE>
<DL>
<DD>This method should be implemented by Filters, the method receives the name of the element and its attributes. What the method does depends on the filter.<DD>
<DL>
<DT><B>Parameters:</B><DD>
attribs - This is an array of associative arrays containing the attributes for the element. You can process it using
a construct like: foreach(name=>name)</PRE>
<DL>
<DD>This method should be implemented by filters, it will be called when an XML element ends<DD>
<DL>
<DT><B>Parameters:</B><DD>
data)</PRE>
<DL>
<DD>This method should be implemented by filters, the method will be called when text is found in an XML document, the method can be called several times for the same text node (by chunks) and no context information is provided, the filter should track context if it needs to know, for example, the name of the element where text was found<DD>
<DL>
<DT><B>Parameters:</B><DD>
xmlfile)</CODE>
<BR>
Constructor</TD>
</TR>
<!-- A ROW -->
<!-- A ROW -->
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE> void</CODE></FONT></TD>
<TD><CODE><B><A HREF="#class25method2">parse</A></B>(string obj)</CODE>
<BR>
Sets the listener object of the ExpatParser</TD>
</TR>
<!-- A ROW -->
<!-- A ROW -->
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE> void</CODE></FONT></TD>
<TD><CODE><B><A HREF="#class25method4">parserSetOption</A></B>(constant value)</CODE>
<BR>
Sets options for the Expat parser</TD>
</TR>
<!-- A ROW -->
</TABLE>
<P>
<!-- METHOD SUMMARY -->
<!-- ============ METHOD DETAIL ========== -->
<A NAME="method_detail"><!-- --></A>
<TABLE BORDER="1" CELLPADDING="3" CELLSPACING="0" WIDTH="100%">
<TR BGCOLOR="#CCCCFF" CLASS="TableHeadingColor">
<TD COLSPAN=1><FONT SIZE="+2">
<B>Method Detail</B></FONT></TD>
</TR>
</TABLE>
<!-- method -->
<A NAME="#class25method1"><!-- --></A>
<H3>ExpatParser</H3>
<PRE>
void <B>ExpatParser</B>(string xmlfile - Name of the file containing the XML document to be parsed<br><DT><B>Returns:</B><DD><DT><B>Throws:</B><DD><CODE>None</CODE>
</DL>
</DD>
</DL>
<HR>
<!-- a method -->
<!-- method -->
<A NAME="#class25method2"><!-- --></A>
<H3>parse</H3>
<PRE>
void <B>parse</B>(string obj)</PRE>
<DL>
<DD>This method is used to set the listenerObject for the parser: the first Filter in the chain. The object must be an instance of a class extending the AbstractFilter object.<DD>
<DL>
<DT><B>Parameters:</B><DD>
(C) Æliens
20/2/2008
You may not copy or print any of this material without explicit permission of the author or the publisher.
In case of other copyright issues, contact the author.