home *** CD-ROM | disk | FTP | other *** search
/ Enter 2004 June / ENTER.ISO / files / xampp-win32-1.4.5-installer.exe / xampp / Readme < prev    next >
Encoding:
Text File  |  2004-03-24  |  8.1 KB  |  208 lines

  1. $Id: Readme,v 1.4 2003/10/08 10:15:40 harryf Exp $
  2. ++Introduction
  3. XML_HTMLSax is a SAX based XML parser for badly formed XML documents,
  4. such as HTML.
  5.  
  6. The original code base was developed by Alexander Zhukov and published at
  7. http://sourceforge.net/projects/phpshelve/. Alexander kindly gave permission
  8. to modify the code and license for inclusion in PEAR.
  9.  
  10. PEAR::XML_HTMLSax provides an API very similar to the native PHP Expat
  11. extension, allowing handlers using one to be easily adapted to the other.
  12. The key difference is HTMLSax will not break on badly formed XML, allowing
  13. it to be used for parsing HTML documents. Otherwise HTMLSax supports all
  14. the handlers available from Expat except namespace and external entity
  15. handlers. Provides methods for handling XML escapes as well as JSP/ASP
  16. opening and close tags.
  17.  
  18. Version 2 has had it's internals completely overhauled to use a Lexer,
  19. delivering performance *approaching* that of the native XML extension,
  20. as well as a radically improved, modular design that makes adding
  21. further functionality easy.
  22.  
  23. The public API has remained the same as older versions, except for the
  24. set_option() method, the available options having been renamed with additional
  25. options which allow HTMLSax to behave almost exactly like the native Expat
  26. extension, allowing contents of XML elements which contain linefeeds, tabs
  27. and XML entities to trigger additional calls to the registered data handler,
  28. as defined by XML_HTMLSax::set_data_handler().
  29.  
  30. A big thanks to Jeff Moore (lead developer of WACT:
  31. http://wact.sourceforge.net) who's largely responsible for new design, as well
  32. input from members at Sitepoint's Advanced PHP forums:
  33. http://www.sitepointforums.com/showthread.php?threadid=121246.
  34.  
  35. Thanks also to Marcus Baker (lead developer of SimpleTest:
  36. http://www.lastcraft.com/simple_test.php) for sorting out the unit tests.
  37.  
  38. ++Uses
  39. Some particular situations where XML_HTMLSax can be useful include;
  40. - Parsing XML documents (such as those online) where the source is
  41.   out of your control and Expat is choking because it's badly formed.
  42. - Converting HTML to XHTML
  43. - Filtering form posts to allow a limited HTML subset (probably with
  44.   help from PEAR::XML_SaxFilters)
  45. - Reading HTML based content from a database and converting to PDF (with
  46.   help from a PDF generation library and probably PEAR::XML_SaxFilters as
  47.   well)
  48. - Parsing ASP(.NET) and JSP pages.
  49. - Creating a PHP-GTK based web browser? A PHP CSS Parser exists:
  50.   http://www.phpclasses.org/browse.html/package/1081.html
  51.  
  52. ++Features
  53. - Won't "break" on badly formed XML. May in some instances get it "wrong"
  54.   (see Limitations) but will continue parsing.
  55.  
  56. - Provides an API similar to the native PHP XML Expat extension
  57.  
  58. - Can be instructed to behave in more or less the same manner as Expat,
  59.   when dealing with linefeeds, tabs and XML entities
  60.  
  61. - In addition to handling basic XML elements attributes and data also
  62.   capable of dealing with; 
  63.    - Processing instructions e.g. <?php ?> / <?xml ?> etc. Within PI's
  64.      XML entities are not parsed (i.e. ignore < and > )
  65.    - XML Escape markup such as <! >, <!-- --> and <![CDATA[ ]]>. Within
  66.      this XML entities are not parsed (useful for JavaScript, for example)
  67.    - JSP / ASP (JASP) marked up with <% %>. Note: You will need to
  68.      deal with <%@ %> and <%= %> yourself. With JASP markup XML entities
  69.      are not parsed
  70.  
  71. ++Usage Notes
  72.  
  73. - Performance-wise, it runs faster on PHP 4.3.0 thanks to strspn() and
  74.   strcspn() supporting position arguments. For older PHP versions while
  75.   loops are used to achieve the same effect, meaning a slightly higher
  76.   overhead. Note also that setting XML options with XML_HTMLSax::set_option()
  77.   also slows down the parser, the options being handled by "decorators"
  78.   which perform some further formatting on XML events which have already
  79.   been parsed.
  80.  
  81. - By default, no parser options are set
  82.  
  83. - Regarding the XML_OPTION_ENTITIES_PARSED, this uses the html_entity_decode()
  84.   function which is only available in PHP 4.3.0+. To get round this, HTMLSax
  85.   checks your PHP version and for the function name html_entity_decode. If not
  86.   found, it defines a function which mirrors the behavior of the native PHP
  87.   html_entity_decode().
  88.   Both XML_OPTION_ENTITIES_PARSED and XML_OPTION_ENTITIES_UNPARSED can be used
  89.   down to PHP version 4.0.5, due to the regular expression used to find entities.
  90.  
  91. - For attributes which have just a name but no value e.g.
  92.   <option value="bar" selected>
  93.  
  94.   HTMLSax will return a boolean true value for that attribute name, when
  95.   calling the opening tag handler;
  96.  
  97.   function myOpenHandler($parser,$name,$attrs) {
  98.       print_r ( $attrs );
  99.   }
  100.  
  101.   This would produce;
  102.  
  103.   Array
  104.   (
  105.       [value] => bar
  106.       [selected] => true
  107.   )
  108.  
  109. - JASP directives like <%@ and <%= will be not be regarded as special.
  110.   I.e. you will get back the @ or % from the contents of the JASP block
  111.   and have to deal with these yourself.
  112.  
  113. ++ Limitations
  114. - XML_HTMLSax only supports use of PHP classes as callback handlers;
  115.   there is no support for using PHP functions as handlers.
  116.  
  117. - The only weird behaviour is for attributes which only have a left quote
  118.   or apostrophe e.g. <tag foo="bar>Some Text</tag> will give you an
  119.   attribute like $attrs['foo']="bar>Some Text"; This is a trade off against
  120.   allowing XML entities like < and > to appear inside attributes.
  121.  
  122. - Although the package name might suggest otherwise, XML_HTMLSax has no
  123.   special knowledge of HTML (i.e. there is no understanding of whether
  124.   a given HTML document is well formed or not, according to HTML's rules).
  125.   XML_HTMLSax is simply intended as a SAX based parser that will not
  126.   complain about structure of a document it is asked to parse. It even does
  127.   a fair job of parsing http://static.php.net/www.php.net/images/php.gif ...
  128.  
  129. - <script /> elements containing < or > characters; these will be treated as new
  130.   elements triggering the listeners. Make sure that any JavaScript inside is marked
  131.   either with an XML comment <!-- --> or a CDATA block <[CDATA[ ]]>
  132.   </script>
  133.   Alternatively define open / close handlers which watch for <script /> elements
  134.   as a special case, so that any further events triggered within them are handled
  135.   as part of the <script /> element.
  136.  
  137. - If you change the handlers once parsing has started, you will need to re-set
  138.   and parser options you have defined
  139.  
  140. ++ Example Use
  141. Further examples are available in the examples directory of this package.
  142.  
  143. <?php
  144. // Include HTMLSax
  145. require_once('XML/XML_HTMLSax.php');
  146.  
  147. // Define a customer handler class
  148. class MyHandler {
  149.     function MyHandler(){}
  150.  
  151.     // Opening tags
  152.     function openHandler(& $parser,$name,$attrs) {
  153.         echo ( 'Open Tag Handler: '.$name );
  154.         echo ( 'Attrs:' );
  155.         print_r($attrs);
  156.     }
  157.  
  158.     // Closing tags
  159.     function closeHandler(& $parser,$name) {
  160.         echo ( 'Close Tag Handler: '.$name );
  161.     }
  162.  
  163.     // Text node handler
  164.     function dataHandler(& $parser,$data) {
  165.         echo ( 'Data Handler: '.$data );
  166.     }
  167.  
  168.     // XML escape handler (e.g. HTML comments)
  169.     function escapeHandler(& $parser,$data) {
  170.         echo ( 'Escape Handler: '.$data );
  171.     }
  172.  
  173.     // Processing instruction handler
  174.     function piHandler(& $parser,$target,$data) {
  175.         echo ( 'PI Handler: '.$target.' - '.$data );
  176.     }
  177.  
  178.     // JSP / ASP markup handler
  179.     function jaspHandler(& $parser,$data) {
  180.         echo ( 'Jasp Handler: '.$data );
  181.     }
  182. }
  183.  
  184. // Get some HTML document
  185. $doc = file_get_contents('http://www.php.net');
  186.  
  187. // Instantiate the handler
  188. $handler=new MyHandler();
  189.  
  190. // Instantiate the parser
  191. $parser=& new XML_HTMLSax();
  192.  
  193. // Register the handler with the parser
  194. $parser->set_object($handler);
  195.  
  196. // Set a parser option
  197. $parser->set_option('XML_OPTION_TRIM_DATA_NODES');
  198.  
  199. // Set the callback handlers (MyHandler methods)
  200. $parser->set_element_handler('openHandler','closeHandler');
  201. $parser->set_data_handler('dataHandler');
  202. $parser->set_escape_handler('escapeHandler');
  203. $parser->set_pi_handler('piHandler');
  204. $parser->set_jasp_handler('jaspHandler');
  205.  
  206. // Parse the document
  207. $parser->parse($doc);
  208. ?>