Location: PHPKode > scripts > Path Parser > path-parser/class_path_parser.html
<html>
<head>
<title>Path Parser (class_path_parser.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">Path Parser (class_path_parser.php)</td>
</tr>
<tr>
  <td bgcolor="#aaaaee" class="textblbl" align="center"><a href="http://www.sourceforge.net/projects/phpxmlclasses">Downloads at Sourceforge</a></td>
</tr>
</table>
<br/>
<table border="0" width="80%">
<tr>
  <td bgcolor="#eeeeff" class="text">Description: A generic XML parsing class that lets your set-up PHP functions
  to be called when some XML elements are found. <br/>Using this class you can set handlers for specific XML paths,
  for example /foo/data/name, for each handler you set up a PHP function in the form foo($name,$attribs,$content) that
  the parser will call when an element matching the given path is found by the parser. <br/>
  Summarizing this is a good tool to generalize parsing tasks using SAX/expat from PHP. It can be used as a generic
  parsing class and is specially useful to retrieve elements from a very large file before processing each element
  using DOM. (We've parsed 50Mb XML documents with success using this class).
  </td>
</tr>
</table>
<br/>
<table border="0" bgcolor="ddddff" width="80%">
<tr>
  <td valign="top" class="text" width="50%"><b>NEWS:</b>
  <ul>
    <li> 06-20-2002 First version of this class 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/path_parser/">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>Absolute paths can be parsed.
    <li>Multiple handlers can be set, a handler can handle multiple paths.
    <li>Namespaces are supported (local name can be used regardless of the namespace)
  </ul>
  </td>
  <td valign="top" bgcolor="ddddff" class="text" width="50%">
  <ul>
    <li>Support for paths with wildcards is planned example //name or /foo//name
  </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:hide@address.com">hide@address.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/>
See path_parser.php for an example about how to use the class, this is the code:
<br/><br/>
<pre>
include_once("class_path_parser.php");

function name($name,$attribs,$content) {
  print("<br/>");
  print("Hey $name <br/>\n");
  print_r($attribs);
  print("<br/>");
}

$parser = new Path_parser();
$parser->set_handler("/foo/data/name","name");
$parser->set_handler("/foo/data","name");
$parser->set_handler("/foo/data/type/var","name");
if(!$parser->parse_file("foo.xml")) {
  print("Error:".$parser->get_error()."<br/>\n"); 
}
</pre>
<br/><br/>
As you can see using this class is very easy and can provide a lot of very good results. If the document
uses namespaces you can set-up paths based on local-names. (A handler setting paths using qualified names
is planned for a future release)
</div>
<h3>Documentation</h3>
<h2>Classes</h2>
<ul>
  <li><a href="#class31">Path_parser</a></li>
</ul>

<a name="class31"></a>
<h3>Path_parser</h3>
Extends: None<hr>
Description: Using this class you can parse an XML file setting handlers for specific XML elements defined by paths, for example /foo/data/name or /foo/data, the handlers can receive the element name, attributes and content. This class can be used in several ways incresing XML processing felxibility a lot.<hr>
<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>&nbsp;string</CODE></FONT></TD>
          <TD><CODE><B><A HREF="#class31method1">get_error</A></B>()</CODE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Returns last error message</TD>
</TR>
<!-- A ROW -->
<!-- A ROW -->
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>&nbsp;void</CODE></FONT></TD>
          <TD><CODE><B><A HREF="#class31method2">init</A></B>()</CODE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Initializes the parser</TD>
</TR>
<!-- A ROW -->
<!-- A ROW -->
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>&nbsp;boolean</CODE></FONT></TD>
          <TD><CODE><B><A HREF="#class31method3">parse_file</A></B>(string $xml)</CODE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Parses an XML document from a file or URL</TD>
</TR>
<!-- A ROW -->
<!-- A ROW -->
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>&nbsp;boolean</CODE></FONT></TD>
          <TD><CODE><B><A HREF="#class31method4">parse</A></B>(string $data, boolean $is_final)</CODE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Parses data</TD>
</TR>
<!-- A ROW -->
<!-- A ROW -->
<TR BGCOLOR="white" CLASS="TableRowColor">
<TD ALIGN="right" VALIGN="top" WIDTH="1%"><FONT SIZE="-1">
<CODE>&nbsp;void</CODE></FONT></TD>
          <TD><CODE><B><A HREF="#class31method5">set_handler</A></B>(string $path, string $handler_name)</CODE>
<BR>
&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Sets a handler to process XML elements</TD>
</TR>
<!-- A ROW -->
</TABLE>
&nbsp;
<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="class31method1"><!-- --></A>
<H3>get_error</H3>



<PRE>
string <B>get_error</B>()</PRE>
               
<DL>
<DD>This function can be used to return last error message when something went wrong.<DD>&nbsp;
<DL>
<DT><B>Parameters:</B><DD>
                          <DT><B>Returns:</B><DD>The error message<DT><B>Throws:</B><DD><CODE>None</CODE>
</DL>
</DD>
</DL>
<HR>
<!-- a method -->
<!--  method -->
<A NAME="class31method2"><!-- --></A>
<H3>init</H3>



<PRE>
void <B>init</B>()</PRE>
               
<DL>
<DD>This method must be called if you plan to parse more than one document using the same object, after parsing a document call init and you are ready to parse a new document.<DD>&nbsp;
<DL>
<DT><B>Parameters:</B><DD>
                          <DT><B>Returns:</B><DD>Nothing<DT><B>Throws:</B><DD><CODE>None</CODE>
</DL>
</DD>
</DL>
<HR>
<!-- a method -->
<!--  method -->
<A NAME="class31method3"><!-- --></A>
<H3>parse_file</H3>



<PRE>
boolean <B>parse_file</B>(string $xml)</PRE>
               
<DL>
<DD>This method can be used to parse an XML document from a file or URL<DD>&nbsp;
<DL>
<DT><B>Parameters:</B><DD>
                          $xml - URI or name of the file containing the document to be parsed<br><DT><B>Returns:</B><DD>True if the document was parsed succesfully, false if there was some error.<DT><B>Throws:</B><DD><CODE>If an error occurss this method sets an error message that can be recovered with get_error</CODE>
</DL>
</DD>
</DL>
<HR>
<!-- a method -->
<!--  method -->
<A NAME="class31method4"><!-- --></A>
<H3>parse</H3>



<PRE>
boolean <B>parse</B>(string $data, boolean $is_final)</PRE>
               
<DL>
<DD>This is a generic parsing method it can be used to parse chunks of data.<DD>&nbsp;
<DL>
<DT><B>Parameters:</B><DD>
                          $data - This should contain a chunk of XML data to be parsed<br>$is_final - This is a boolean var indicating if the chunk passed as the previous argument was the last chunk of data to be parsed by the parser<br><DT><B>Returns:</B><DD>True if the data was parsed succesfully or false if there was some error<DT><B>Throws:</B><DD><CODE>None</CODE>
</DL>
</DD>
</DL>
<HR>
<!-- a method -->
<!--  method -->
<A NAME="class31method5"><!-- --></A>
<H3>set_handler</H3>



<PRE>
void <B>set_handler</B>(string $path, string $handler_name)</PRE>
               
<DL>
<DD>This method can be used to process XML elements that match a given pattern, for example /foo or /foo/name or /foo/data/name, etc. You set-up the name of a PHP function to be called when the element is parsed.<DD>&nbsp;
<DL>
<DT><B>Parameters:</B><DD>
                          $path - This is an absolute path from the roor of the XML document for example /foo/data/name will match name elements children of data children of foo (foo is the root element).<br>$handler_name - The handler must receive the following arguments: $name,$attribs and $content. $name will be the name of the element. $attribs is an array of asocs containing the element attributes and $content will be a string with the element content (text and subelements)<br><DT><B>Returns:</B><DD>Nothing<DT><B>Throws:</B><DD><CODE>None</CODE>
</DL>
</DD>
</DL>
<HR>
<!-- a method -->
<!-- ============ METHOD DETAIL ========== -->
</td>
</tr></table>
</body>
</html>
Return current item: Path Parser